开发者

Erasing after drawing with CGContext

I'm trying to do a simple drawing app for the iPad where you can draw on a picture, and I'm using CGContext stuff to do it but the way I originally planned on handling erasing was to just dr开发者_StackOverflowaw over stuff with white...except I just realized today that it doesn't work when you're drawing onto another image because then when you "erase" you'll also "erase" the background image as well.

Is there any way to support actual erasing?

Thanks!


I also needed erasing functionality. Based on @Jeremy's answer, here is what worked for me:

CGContextRef cgref = UIGraphicsGetCurrentContext();

if(erase == TRUE) // Erase to show background
{
    CGContextSetBlendMode(cgref, kCGBlendModeClear);
}
else // Draw with color
{
    CGContextSetBlendMode(cgref, kCGBlendModeNormal);
}


Display the user's drawing in a layer above the image. Then erasing is as simple as drawing a transparent patch on the drawing layer in order to let the image pixels below it show through.


Clear all CGContextRef drawings:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, self.bounds);
[self setNeedsDisplay];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜