When I combine two UIImage's (using drawinrect:) they are both the same alpha even though one is supposed to be less than 1.0. How can I change the alpha of specifically a UIImage?
Asked
Active
Viewed 2,501 times
3 Answers
1
You can't change the alpha of a UIImage. You could draw it with alpha to a new context and get a new image from that. Or you could extract the CGImage, then extract the data, then adjust the alpha bytes, then create a new CGImage from the data and a new UIImage from the CGImage.
But in this case, just use drawInRect:blendMode:alpha:
instead of drawInRect:
.

Anomie
- 92,546
- 13
- 126
- 145
0
Here is UIImage Category.
usage >>
UIImage * imgNew = [imgOld cloneWithAlpha:.3];
code >>
- (UIImage *)cloneWithAlpha:(CGFloat) alpha {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
0
If the UIImage is being displayed inside a UIImageView you can set the alpha property on the UIImageView.

progrmr
- 75,956
- 16
- 112
- 147