How to draw a shape on top of a UIImage while respecting the image's alpha mask
发布日期:2021-06-24 18:14:09 浏览次数:2 分类:技术文章

本文共 3146 字,大约阅读时间需要 10 分钟。

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: 

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.

 

 

 

 

 

 

转载地址:https://blog.csdn.net/weixin_34248258/article/details/86074301 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Qt读写INI配置文件
下一篇:Directx11教程41 纹理映射(11)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月14日 18时14分46秒