1

I'm working on an photo app where you can draw lines on an photo. The background is the photo, and via drawrect I draw lines on a subview.

Every thing works but how can I erase lines on a subview (like an eraser), I tried with clearColor as setStroke.

Is it possible to erase the lines like this.

Martin
  • 271
  • 4
  • 9
  • Please see this link http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting-application-by-quartz/12797513#12797513 – Waseem Shah Oct 09 '12 at 10:42

3 Answers3

2

You have to set the blend mode to kCGBlendModeCopy if you want drawing with a fully transparent color to have any effect.

This is normally done with the CGContextSetBlendMode function. If you use UIBezierPath for drawing, you could also use the strokeWithBlendMode:alpha: method.

omz
  • 53,243
  • 5
  • 129
  • 141
1

An implementation of the response given by @omz could be:
(assuming imgBlank is a UIImageView placed on top of the main view of the viewcontroller)

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.imgBlank];
    NSValue *valCurrPoint=[NSValue valueWithCGPoint:CGPointMake(currentPoint.x, currentPoint.y)];
    [self.dots addObject:valCurrPoint];
    [self performSelector:@selector(draw:) withObject:valCurrPoint afterDelay:0];
}

-(void)draw:(NSValue*)pointz{
    CGPoint point=[pointz CGPointValue];
    UIGraphicsBeginImageContextWithOptions(self.imgBlank.frame.size,NO,0);
    [self.imgBlank.image drawInRect:CGRectMake(0, 0, self.imgBlank.frame.size.width, self.imgBlank.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),tickness);//set the tickness here
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0f, 1.0f, 1.0f, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());  
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), _lastPoint.x, _lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.imgBlank.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    _lastPoint = point;
}
Mat
  • 7,613
  • 4
  • 40
  • 56
0

I tried using @omz's second suggestion of doing this in UIBezierPath using strokeWithBlendMode:alpha: and it worked great. Here is the code:

if (pen) {
    [self updateColorFromPen];
    [path setLineWidth:3.0];
} else if (eraser) {
    [[UIColor clearColor] setStroke];
    [path setLineWidth:42.0];
    [path strokeWithBlendMode:kCGBlendModeCopy alpha:1];
}
roro
  • 265
  • 3
  • 11