I am using CoreGraphics to implement free hand drawing on a background image which is working fine for me and now I want to implement erase feature for this drawing so that user can clear his drawing
Asked
Active
Viewed 231 times
2 Answers
0
If all you want to do is clear the screen, delete the CGPaths you've created and it will draw nothing.
If you're looking to let the use switch to an eraser tool and only erase certain things, you need to set up a new drawing class that lets you draw the background over top your other drawings or adjust the class you have to not draw intersections between original paths and the eraser path.

Ryan Poolos
- 18,421
- 4
- 65
- 98
-
i dint get your point please explain it in a other way or clearly ....and yes i want to erase only certain things. – virantporwal Jan 16 '13 at 13:42
-
To erase only certain things, the easiest way is to start a new CGPath, and change its color to match the background. So you're overwriting what was there before and it looks like its erasing. – Ryan Poolos Jan 16 '13 at 13:44
-
The harder, more robust way however is to check intersections between you "Color" CGPaths and your "Eraser" CGPaths. If they intersect don't draw there because its been erased. – Ryan Poolos Jan 16 '13 at 13:45
-
The last way is to rasterize your drawings into a UIImageView, then when erasing literally cut out the UIImage. But this won't allow you to edit your drawings once they've been rasterized. – Ryan Poolos Jan 16 '13 at 13:46
-
2first option is good but my background is combination of black and white color..and please so me any example for that... – virantporwal Jan 16 '13 at 13:50
-
Tell me if i have a button for eraser then how can i do that by clicking that button – virantporwal Jan 16 '13 at 14:48
0
you can use this in the UITouch methods"touchesMoved" to clear the path you have drawn
`UITouch *touch = [[event allTouches] anyObject];
currenttouch = [touch locationInView:drawingImageView];
CGColorRef strokeColor = [UIColor blackColor].CGColor;
UIGraphicsBeginImageContext(drawingImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawingImageView.image drawInRect:CGRectMake(0, 0, drawingImageView.frame.size.width, drawingImageView.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, brushSize);
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, currenttouch.x, currenttouch.y);
CGContextAddLineToPoint(context, currenttouch.x, currenttouch.y);
CGContextStrokePath(context);
drawingImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
`

ankit yadav
- 363
- 2
- 13
-
tell me how can i post my code .i am doing here but it shows some error – virantporwal Jan 16 '13 at 14:13
-
-
sorry but nahi ho raha hai tell me example i am using this firs time – virantporwal Jan 16 '13 at 14:36
-
thanx ankit but this code is not working.when i am using this code lines are erase and my backgroun image also erase and i dont want erase background image – virantporwal Jan 18 '13 at 06:44