问题描述
iOS SDK上有没有办法用彩色像素覆盖图像中的非透明像素?
Is there any way on the iOS SDK to overlay the non-transparent pixels in an image with colored pixels?
非常感谢两位回答。
我实现的最终解决方案使用了在子类UIView的drawRect方法中接受的答案中提到的代码,我使用了以下内容覆盖颜色的代码:
The final solution I implemented used the code mentioned in the accepted answer within the drawRect method of a subclassed UIView, I used the following code to overlay the color:
CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor));
CGContextFillRect(context, area);
推荐答案
我想你可能正在寻找混合模式 kCGBlendModeSourceAtop 即可。首先,将UIImage绘制到您的视图中。然后使用
I think you are probably looking for the blend mode kCGBlendModeSourceAtop. First, draw the UIImage into your view. Then obtain the current graphics context with
CGContext currentCtx = UIGraphicsGetCurrentContext();
然后保存上下文状态,并设置混合模式:
Then save the context state, and set the blend mode:
CGContextSaveGState(currentCtx);
CGContextSetBlendMode(currentCtx, kCGBlendModeSourceAtop);
然后你可以在UIImage的不透明像素上绘制任何想要覆盖的内容。之后,只需重置上下文状态:
Then you can draw whatever you want to be overlaid over the opaque pixels of the UIImage. Afterward, just reset the context state:
CGContextRestoreGState(currentCtx);
希望这有帮助!
这篇关于在iOS Cocoa中叠加非透明像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!