如何在 UIImage 顶部绘制形状,同时尊重图像的 alpha 蒙版
我需要一个可以根据标志以彩色或黑白方式绘制自身的 UIImageView:
BOOL isGrey;
我尝试通过在原始图像顶部绘制一个黑色矩形并将 Quartz 混合模式设置为“颜色”来实现。这是可行的,只是它不尊重图像的 alpha 蒙版。
见图: 替代文本 http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png
搜索 Google 并所以,我找到并尝试了几种解决方案,但都不尊重面具。
以下是生成上面“我得到的”图像的代码:
- (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 编程指南,但是很难从重叠和超链接的主题中提取您需要的一点信息。
显然,我正在寻找一种通用的解决方案,该解决方案适用于具有任何遮罩形状的图像,而不仅仅是显示的圆圈。
I need a UIImageView that can draw itself in color or b/w according to a flag:
BOOL isGrey;
I'm trying to do it by drawing a black rectangle on top of the original image with the Quartz blendmode set to Color. This works except it doesn't respect the image's alpha mask.
See illustration:
alt text http://img214.imageshack.us/img214/1407/converttogreyscaleillo.png
Searching Google and SO, I found and tried several solutions but none respect the mask either.
Here is the code that produces the 'What I get' image above:
- (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];
}
}
Is there some Quartz drawing mode that I'm forgetting to set? I've looked thru the Quartz Programming Guide but is so hard to extract the one bit of info you need from the overlapping and hyperlinked subjects.
Obviously I'm looking for a general solution that will apply to images with any masked shape, not just the circle shown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在尊重图像的 Alpha 蒙版的同时绘制形状,只需在绘制之前添加一行:
}
To draw a shape while respecting an image's alpha mask, just add one line before you draw:
}
虽然它没有直接回答您的问题,但您也许可以通过使用 kCGBlendModeLuminosity 混合模式获得您正在寻找的效果:
此外,传递给
drawRect 的矩形:
是无效区域,不是整个区域。您应该改用bounds
Although it doesn't directly answer your question, you may be able to get the effect you are looking for by using the
kCGBlendModeLuminosity
blend mode:Also, the rect passed to
drawRect:
is the invalid area, not the entire area. You should be using thebounds
instead我会采取简单的方法,画一个与圆形图像大小相同的黑色圆圈。可能不是你所要求的。
I'd take the easy way out and draw a black circle with the same size as the round image you have. Probably not what you are asking for.
您可以尝试向视图的核心动画层添加蒙版。
另一种想法。为什么你不能只创建图像的黑白版本并根据你的标志进行交换?
You could try to add a mask to the view's Core Animation layer.
One other thought. Why can't you just create a b&w version of the image and swap that out depending on your flag?