Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove alpha channel from UIImage

I use the following method to get a decompressed uiimage from file system. However the UIImageView is colored as red when I turn on the color blended layer, even though the UIImageView is set to Opaque.

The images on file system don't have alpha channel. I tried set CGContextSetAlpha(bitmapContext, 1), but still has blended layer.

Anyone know how to remove alpha channel when using CGContextDrawImage?

- (UIImage *)decompressedImage
{
    CGImageRef imageRef = self.CGImage;
    CGRect rect = CGRectMake(0.f, 0.f, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
                                                       );
    // kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little are the bit flags required
    //  so that the main thread doesn't have any conversions to do.

    CGContextDrawImage(bitmapContext, rect, imageRef);

    CGImageRef decompressedImageRef = CGBitmapContextCreateImage(bitmapContext);
    UIImage* decompressedImage = [UIImage imageWithCGImage:decompressedImageRef
                                                     scale:[[UIScreen mainScreen] scale]
                                               orientation:UIImageOrientationUp];
    CGImageRelease(decompressedImageRef);
    CGContextRelease(bitmapContext);

    return decompressedImage;
}
like image 948
Jensen Avatar asked Jan 28 '14 20:01

Jensen


People also ask

How do you know if an image is alpha channel?

To check if the image has an alpha channel, go to the channel dialog and verify that an entry for “Alpha” exists, besides Red, Green and Blue. If this is not the case, add a new alpha channel from the layers menu; Layer+Transparency → Add Alpha Channel.

How do I change the opacity of an image in Swift?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.


2 Answers

In the options for the context, try the option

kCGImageAlphaNoneSkipLast

or

kCGImageAlphaNoneSkipFirst

Depending on if you are using RGBA or ARGB

ie,

CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       rect.size.width,
                                                       rect.size.height,
                                                       CGImageGetBitsPerComponent(imageRef),
                                                       CGImageGetBytesPerRow(imageRef),
                                                       CGImageGetColorSpace(imageRef),
                                                       kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Little
                                                       );
like image 182
Jack Avatar answered Sep 19 '22 20:09

Jack


Same result but less code and things to worry about:

func removeAlpha(from inputImage: UIImage) -> UIImage {
    let format = UIGraphicsImageRendererFormat.init()
    format.opaque = true //Removes Alpha Channel
    format.scale = inputImage.scale //Keeps original image scale.
    let size = inputImage.size
    return UIGraphicsImageRenderer(size: size, format: format).image { _ in
        inputImage.draw(in: CGRect(origin: .zero, size: size))
    }
}
like image 21
Justin Ganzer Avatar answered Sep 17 '22 20:09

Justin Ganzer