How to erase lines in a preview

I am working on a photo application where you can draw lines on a photo. The background is a photograph, and through drawrect I draw lines in a preview.

Everything works, but how can I erase lines in a subtitle (like an eraser), I tried with clearColor as setStroke.

Is it possible to erase lines like this.

+1
source share
3 answers

The implementation of the answer given by @omz could be:
(assuming imgBlank is a UIImageView placed on top of the main 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; } 
+1
source

You need to set the blending mode to kCGBlendModeCopy if you want to have an effect with a fully transparent color.

This is usually done using the CGContextSetBlendMode function. If you use UIBezierPath for drawing, you can also use the strokeWithBlendMode:alpha: method.

+2
source

I tried using @omz second suggestion to do this in UIBezierPath using strokeWithBlendMode: alpha: and it worked fine. 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]; } 
0
source

All Articles