[英]How can I set the Alpha of a UIImage?
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. 当我合并两个UIImage时(使用drawinrect:),它们都是相同的alpha值,即使一个值小于1.0。 How can I change the alpha of specifically a UIImage?
如何更改专门用于UIImage的Alpha?
You can't change the alpha of a UIImage. 您不能更改UIImage的Alpha。 You could draw it with alpha to a new context and get a new image from that.
您可以使用alpha将其绘制到新的上下文并从中获得新的图像。 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.
或者,您可以提取CGImage,然后提取数据,然后调整alpha字节,然后根据数据创建新的CGImage,并根据CGImage创建新的UIImage。
But in this case, just use drawInRect:blendMode:alpha:
instead of drawInRect:
. 但是在这种情况下,只需使用
drawInRect:blendMode:alpha:
而不是drawInRect:
。
Here is UIImage Category. 这是UIImage类别。
usage >> 用法>>
UIImage * imgNew = [imgOld cloneWithAlpha:.3]; 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? ref >> 如何设置UIImage的不透明度/ alpha?
如果UIImage显示在UIImageView中,则可以在UIImageView上设置alpha属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.