【发布时间】:2011-11-12 06:11:18
【问题描述】:
当我组合两个 UIImage(使用 drawinrect:) 时,它们都是相同的 alpha,即使一个应该小于 1.0。如何更改特定 UIImage 的 alpha?
【问题讨论】:
标签: iphone uiimage core-graphics opacity alpha
当我组合两个 UIImage(使用 drawinrect:) 时,它们都是相同的 alpha,即使一个应该小于 1.0。如何更改特定 UIImage 的 alpha?
【问题讨论】:
标签: iphone uiimage core-graphics opacity alpha
您无法更改 UIImage 的 alpha。您可以使用 alpha 将其绘制到新的上下文中并从中获取新图像。或者您可以提取 CGImage,然后提取数据,然后调整 alpha 字节,然后从数据中创建一个新的 CGImage,并从 CGImage 中创建一个新的 UIImage。
但在这种情况下,只需使用drawInRect:blendMode:alpha: 而不是drawInRect:。
【讨论】:
如果 UIImage 显示在 UIImageView 中,您可以在 UIImageView 上设置 alpha 属性。
【讨论】:
这里是 UIImage 类别。
用法>>
UIImage * imgNew = [imgOld cloneWithAlpha:.3];
代码>>
- (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;
}
【讨论】: