博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to draw a shape on top of a UIImage while respecting the image's alpha mask
阅读量:6643 次
发布时间:2019-06-25

本文共 3206 字,大约阅读时间需要 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://www.cnblogs.com/pengyingh/articles/2470257.html

你可能感兴趣的文章
[转]wait,notify,notifyAll,join,yield,sleep的区别和联系
查看>>
[书目20130216]深入浅出WPF
查看>>
hibernate 一级缓存and 快照
查看>>
iPhone电池到底为何不耐用,真相让人意外
查看>>
真正实现多点触控索尼Xperia Touch试用
查看>>
如何在OpenStack中轻松部署MySQL应用
查看>>
VMware vSphere:架构解析及应用案例
查看>>
可视化探索卷积神经网络提取特征
查看>>
Docker-run
查看>>
一点资讯拆VIE回国;拼多多做跨境海淘;复宏汉霖首款生物类似药获批
查看>>
怎么设计IOS登录页面的跳转关系
查看>>
ecshop 漏洞如何修复 补丁升级与安全修复详情
查看>>
【三】Centos7.4 安装Docker
查看>>
拜腾与博世将在动力系统、驾驶员辅助等方面展开重点合作
查看>>
国资入场,P2P网贷平台星火钱包千万级A+轮融资
查看>>
windows server21012 r2 密钥
查看>>
北大发布新零售之城发展指数报告,上海超北京成榜首
查看>>
python urllib爬取网页编码问题
查看>>
JMS的常用方法
查看>>
隐私与机器学习,二者可以兼得吗?
查看>>