Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

spaceman816

macrumors member
Original poster
Jul 29, 2009
30
0
I've looked all over for this but nobody seems to have posted a working answer:
How can I make an eraser for a drawing made using CGContext?
I can't just draw the background color over the drawing because I need the background to remain transparent.
People have suggested using transparent paint, but that won't work.
I need to replace what's drawn with transparent paint.

Thanks.
 
You need to set the CGBlendMode correctly. I have code that does this (draws a colour then overdraws in transparent) but I'm not at home right now so I can't confirm which blend mode I am using (or that this is what I am doing).
 
CGContextClearRect() might work, but not sure if it would work along with a clipping (for erasing non-rectangles). I would assume it would but haven't tested.
 
CGContextClearRect() might work, but not sure if it would work along with a clipping (for erasing non-rectangles). I would assume it would but haven't tested.

I meant to mention that I tried this out, but it's a really ugly square eraser that doesn't fit my needs. I need a round cap.
 
Trying creating a path, adding it to the context, and using CGContextClip() to clip, then do the erase.
 
I meant to mention that I tried this out, but it's a really ugly square eraser that doesn't fit my needs. I need a round cap.

And, of course, you've investigated the solution I proposed? Thought not.

This is what I do:

Code:
	CGContextSetStrokeColorWithColor(c, [[UIColor clearColor] CGColor]);
	CGContextSetFillColorWithColor(c, [[UIColor clearColor] CGColor]);
	CGContextSetBlendMode(c, kCGBlendModeClear);
	CGContextAddEllipseInRect(c,CGRectMake(size.width-(size.width/2)-(size.width/10), (size.width/10), (size.width/2), (size.height/2)));
	CGContextClosePath(c);
	CGContextDrawPath(c, kCGPathFillStroke);
 
And, of course, you've investigated the solution I proposed? Thought not.

This is what I do:

Code:
	CGContextSetStrokeColorWithColor(c, [[UIColor clearColor] CGColor]);
	CGContextSetFillColorWithColor(c, [[UIColor clearColor] CGColor]);
	CGContextSetBlendMode(c, kCGBlendModeClear);
	CGContextAddEllipseInRect(c,CGRectMake(size.width-(size.width/2)-(size.width/10), (size.width/10), (size.width/2), (size.height/2)));
	CGContextClosePath(c);
	CGContextDrawPath(c, kCGPathFillStroke);

I hadn't gotten a chance to look into it- but this works perfectly.
Thanks so much.
 
the eraser actually jumps a little too much (if you drag your finger quickly, a circle gets erased here and there with space left in the middle). is there any way to smooth the path?
- sorry if i'm asking dumb questions...im new to objective c and haven't been able to find anything that fixes this.
 
the eraser actually jumps a little too much (if you drag your finger quickly, a circle gets erased here and there with space left in the middle). is there any way to smooth the path?
- sorry if i'm asking dumb questions...im new to objective c and haven't been able to find anything that fixes this.

You will need to track the path and interpolate between the points you are actually picking up to provide a smooth path. There is no built in way to do this: it's 100% up to you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.