Reputation: 657
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?
Upvotes: 0
Views: 2519
Reputation: 1876
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;
}
ref >> How to set the opacity/alpha of a UIImage?
Upvotes: 0
Reputation: 77183
If the UIImage is being displayed inside a UIImageView you can set the alpha property on the UIImageView.
Upvotes: 0
Reputation: 94794
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:
.
Upvotes: 1