Reputation: 1338
Is there any way on the iOS SDK to overlay the non-transparent pixels in an image with colored pixels?
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:
CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:1].CGColor));
CGContextFillRect(context, area);
Upvotes: 4
Views: 3285
Reputation: 649
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);
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);
Hope this helps!
Upvotes: 4
Reputation: 39915
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. 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. 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:]
.
Upvotes: 1