[英]Overlay non transparent Pixels in iOS Cocoa
Is there any way on the iOS SDK to overlay the non-transparent pixels in an image with colored pixels? iOS SDK上有没有办法用彩色像素覆盖图像中的非透明像素?
Thanks very much to both who answered. 非常感谢两位回答。
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: 我实现的最终解决方案使用了在子类UIView的drawRect方法中接受的答案中提到的代码,我使用以下代码来覆盖颜色:
CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor));
CGContextFillRect(context, area);
I think you are probably looking for the blend mode kCGBlendModeSourceAtop . 我想你可能正在寻找混合模式kCGBlendModeSourceAtop 。 First, draw the UIImage into your view.
首先,将UIImage绘制到您的视图中。 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);
Then you can draw whatever you want to be overlaid over the opaque pixels of the UIImage. 然后,您可以绘制任何想要覆盖UIImage的不透明像素的内容。 Afterward, just reset the context state:
之后,只需重置上下文状态:
CGContextRestoreGState(currentCtx);
Hope this helps! 希望这可以帮助!
You can modify how something is drawn using the blend mode. 您可以使用混合模式修改绘制内容的方式。 See http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html%23//apple_ref/doc/c_ref/CGBlendMode for a full list of the blend modes supported by Core Graphics on iOS.
有关支持的混合模式的完整列表,请参阅http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html%23//apple_ref/doc/c_ref/CGBlendMode 。 iOS上的核心图形。 From your description, I think you would be interested in either kCGBlendModeSourceIn , which draws the new content using the old content's alpha value as a mask, or kCGBlendModeSourceAtop , which draws the new content using the old content's alpha as a mask on top of the old content using the new content's alpha value as a mask.
从你的描述,我想你会感兴趣的任何kCGBlendModeSourceIn ,吸引使用旧内容的Alpha值的新内容作为掩模,或kCGBlendModeSourceAtop ,使用旧内容的alpha作为掩模借鉴了老顶新内容内容使用新内容的alpha值作为掩码。 You can set the blend mode for all drawing using
CGContextSetBlendMode
, or you can draw a UIImage with a certain blend mode using -[UIImage drawAtPoint:blendMode:alpha:]
. 您可以使用
CGContextSetBlendMode
为所有绘图设置混合模式,也可以使用-[UIImage drawAtPoint:blendMode:alpha:]
绘制具有特定混合模式的-[UIImage drawAtPoint:blendMode:alpha:]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.