-1

I use this code to draw a line in quartz2d

CGPoint currentPoint = CGPointMake(rascalImage.center.x, rascalImage.center.y);
        currentPoint.y += 10;


        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawingView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

Now how would I go about making an erase function for just where the eraser intersects? I know that I have to erase a certain point from the line (where the eraser touches), I just don't know how to do this so please help!!

Aspyn
  • 647
  • 1
  • 10
  • 25

2 Answers2

1
        UIGraphicsBeginImageContext(imgBlankView.frame.size);
        [imgBlankView.image drawInRect:CGRectMake(0, 0, imgBlankView.frame.size.width, imgBlankView.frame.size.height)];

        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(),lineWidth);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());  
        CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);

        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint1.x, lastPoint1.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        imgBlankView.image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();
Crazy Developer
  • 3,464
  • 3
  • 28
  • 62
  • Hi @CrazyDeveloper, I tried using your code, it works, but when I do a series of undo and redo, suppose I draw some lines and then undo it completely and again redo it completely and then try to erase , it clears the whole rect, how to solve this issue, I have created a different post for this, please have a look http://stackoverflow.com/questions/11502320/eraser-not-working-in-ios-drawing – Ranjit Jul 18 '12 at 06:32
0

I assume you are not drawing on a solid colored background? If you are, just use the same function to draw background-colored lines over the contrasting lines.

But, since you most likely have a complex background: Once you end the graphics context, and get a UIImage from the lines you have drawn, there is no way to erase just the lines. If you are creating some sort of complex drawing app, I would suggest that you use layers, and simply "stack up" layers one on top of the other to create the drawing environment.

Check out this post also: How to erase some portion of a UIImageView's image on iOS?

Community
  • 1
  • 1
James
  • 2,272
  • 1
  • 21
  • 31
  • Have you had a chance to try this out yet? Any questions, comments, etc? Did I answer your question? – James Aug 27 '11 at 13:55
  • sorry for the late reply i used kClearBlendMode as the blend mode or something like that and that made it work – Aspyn Dec 08 '11 at 21:42