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;
}

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
                                                       );

ios - Remove alpha channel from UIImage, Anyone know how to remove alpha channel when using CGContextDrawImage? - (UIImage *)decompressedImage { CGImageRef imageRef = self.CGImage� To remove image alpha channel, we can use ImageMagick application. A Simple Guide to Remove Image Alpha Channel with ImageMagick in Win 10 – ImageMagick Tutorial Removing image alpha channel is very important operation before you convert an image to pdf. If you do not remove it, the alpha channel will cause to fail.

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))
        }
    }

subject:"Remove alpha channel from uiimage", Anyone know how to remove alpha channel when using CGContextDrawImage? - (UIImage *)decompressedImage { CGImageRef imageRef = self.CGImage� PWA to remove alpha channels of PNG images using Web Assembly, to avoid upload of any images. Remove Alpha Channel. upload. The alpha channel of the images is removed as per iOS Apple requirements. No image is ever uploaded, the conversion takes place in your browser via Web Assembly.

-(void)JhagRemoveMethodRepet
{

    switch (Jhagtag)
    {
        case 1:
        {

            if (jhag1.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag1.alpha;
                jhag1.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:0 withObject:@"Mid"];
            }else
            {
            jhag1.alpha=0.0;
            [JRemovetimer invalidate];
             JRemovetimer=nil;
            [Checkjhag replaceObjectAtIndex:0 withObject:@"No"];
            [self checkcompeletewater];
            }

        }
            break;
        case 2:
        {

            if (jhag2.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag2.alpha;
                jhag2.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:1 withObject:@"Mid"];

                if (jhagBrush==YES)
                {
                    BTooth2.alpha=0.0;
                }
            }else
            {
                jhag2.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:1 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 3:
        {

            if (jhag3.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag3.alpha;
                jhag3.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:2 withObject:@"Mid"];
            }else
            {
                jhag3.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:2 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 4:
        {
            if (jhag4.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag4.alpha;
                jhag4.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:3 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    BTooth3.alpha=0.0;
                    SpiderMud.alpha=0.0;
                }
            }else
            {
                jhag4.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:3 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 5:
        {

            if (jhag5.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag5.alpha;
                jhag5.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:4 withObject:@"Mid"];
            }else
            {
                jhag5.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:4 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 6:
        {
            if (jhag6.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag6.alpha;
                jhag6.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:5 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    GreenMud.alpha=0.0;
                }
            }else
            {
                jhag6.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:5 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 7:
        {
            if (jhag7.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag7.alpha;
                jhag7.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:6 withObject:@"Mid"];
            }else
            {
                jhag7.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:6 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 8:
        {
            if (jhag8.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag8.alpha;
                jhag8.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:7 withObject:@"Mid"];

                if (jhagBrush==YES)
                {
                    BTooth1.alpha=0.0;
                }
            }else
            {
                jhag8.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:7 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 9:
        {
            if (jhag9.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag9.alpha;
                jhag9.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:8 withObject:@"Mid"];
            }
            else
            {
                jhag9.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:8 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 10:
        {
            if (jhag10.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag10.alpha;
                jhag10.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:9 withObject:@"Mid"];
            }else
            {
                jhag10.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:9 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;
        case 11:
        {
            if (jhag11.alpha>0.3)
            {
                float alpha1;
                alpha1=jhag11.alpha;
                jhag11.alpha=alpha1-0.1;
                [Checkjhag replaceObjectAtIndex:10 withObject:@"Mid"];
                if (jhagBrush==YES)
                {
                    Yellowmud.alpha=0.0;
                }
            }else
            {
                jhag11.alpha=0.0;
                [JRemovetimer invalidate];
                JRemovetimer=nil;
                [Checkjhag replaceObjectAtIndex:10 withObject:@"No"];
                [self checkcompeletewater];
            }

        }
            break;

        default:
            break;
    }

}

-(void)checkcompeletewater
{
    NSMutableArray *compeletearr=[NSMutableArray arrayWithObjects:@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No",@"No", nil];


    if ([Checkjhag isEqualToArray:compeletearr] && jhagBrush==YES)
    {
        waterClean=YES;
        [self SoundPlay:@"puzzle_right"];

ios - Remove alpha channel from UIImage, Recalll is search engine for programmers, where community can create, curate and access qualitative knowledge, In form of small and precise topics. You can access this command from the image menubar through Layer → Transparency → Remove Alpha Channel. In addition, at the Layer Dialog, you can access it through Remove Alpha Channel of its context pop-up menu.

How to remove opacity but keep the alpha channel of UIImage?, How can I process this so that the drawing data has an alpha of 1.0, but retain the alpha channel (for masking)? TL:DR - I'd like the black area to be a solid,� The Alpha Channel image must have the same size as the current image and be either an 8-bit or a float (32-bit) image. A pixel value of 0 reverts to the unfiltered/unpasted state; a pixel value equal to 'Range' keeps the result of the last filter/paste operation.

alphaInfo, alphaInfo. Returns the alpha channel information for a bitmap image. Availability. iOS 2.0+� RemoveAlphaChannel [image, …] returns image if it does not contain an alpha channel. RemoveAlphaChannel [ image , c ] always returns a "Grayscale" or "RGB" image. RemoveAlphaChannel works with 2D and 3D images.

Swift port of UIImage+Resize, UIImage+Alpha, and UIImage+ , Returns a copy of the given image, adding an alpha channel if it doesn't already have one. func imageWithAlpha() -> UIImage? {. if hasAlpha() {. It is computationally expensive to composite the alpha channel of an image with the alpha channel of a non-opaque image view. The performance impact is further magnified if you use Core Animation shadows, because the shape of the shadow is then based on the contents of the view and must be dynamically computed.

Comments
  • It works. Though I didn't much performance improvement, happy to see the red marks gone. Thanks.
  • @Jensen Removing the alpha channel doesn't really give you performance boosts, because they basically fill it in for you when creating a UIImage. You get the boost of smaller compressed images though so its still a good optimization :)
  • I just found that using kCGImageAlphaNoneSkipLast consumes much more memory than using kCGImageAlphaPremultipliedFirst. Do you have any ideas what causes this?
  • @Jensen My guess is that when you used kCGImageAlphaPremultipliedFirst it treated your image 1/4 smaller, since it counts 4 bytes per color. When you switched over, now it counts 3 bytes per color, and pads in a n extra byte. So it is more correct, but uses 33% more memory. If these numbers seem to match what you see then that should be it
  • Turns out I used the wrong flag. Using kCGImageAlphaNoneSkipFirst solves all the issues. Thanks for your help :]
  • This makes pngData nil
  • Add some comments in the code and explain it. Currently it is a bit disaster.
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. (This post was flagged by at least one user, presumably because they thought an answer without explanation should be deleted. I can't say I entirely disagree here.)