Question

I am writing a method that takes a image and a alpha value then return a uiimage with that alpha value.

I have found some code like this

+ (UIImage *)imageByApplyingAlpha:(UIImage*) originalImage andAlpha:(CGFloat) alpha {
    UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, 0.0f);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, originalImage.size.width, originalImage.size.height);

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);

    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);

    CGContextSetAlpha(ctx, alpha);

    CGContextDrawImage(ctx, area, originalImage.CGImage);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}

but this only work on single image. It will generate malformed GIF when I use the returned image to form GIF.

Please help me to find a easy way to get UIImage with alpha.

Thanks all!

Was it helpful?

Solution

Unfortunately, the GIF format doesn't support alpha channels, so you won't be able to set alpha values on one. Transparency in GIF uses index transparency, which is all or nothing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top