我需要一个可以根据标志以彩色或黑白方式绘制自身的 UIImageView:
BOOL isGrey;
我试图通过在原始图像顶部绘制一个黑色矩形并将石英混合模式设置为颜色来实现此目的。这是可行的,只是它不尊重图像的 alpha 蒙版。
见图:替代文本 http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png
搜索谷歌和SO,我找到并尝试了几种解决方案,但没有一个尊重面具。
以下是生成上面“我得到的”图像的代码:
- (void)drawRect:(CGRect)rect {
if (isGrey) {
CGContextRef context = UIGraphicsGetCurrentContext();
// flip orientation
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// draw the image
CGContextDrawImage(context, self.bounds, self.image.CGImage);
// set the blend mode and draw rectangle on top of image
CGContextSetBlendMode(context, kCGBlendModeSaturation);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rect);
} else {
[self.image drawInRect:rect];
}
}
是否有一些我忘记设置的 Quartz 绘图模式?我已经阅读了 Quartz 编程指南,但是很难从重叠和超链接的主题中提取您需要的一点信息。
显然,我正在寻找一种通用的解决方案,该解决方案适用于具有任何遮罩形状的图像,而不仅仅是显示的圆圈。