문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top