Ios - it's impossible to erase UIImage like rubber

In my application, I have two views one above the other. I look below, I have a captured image, and in the above view I posted the image. If the user needs to make some changes to the image above, I give them the option to erase the image.

Everything works fine, the problem is that I am trying to erase an image that seems broken. Image is not deleted in a gentle way. when I wash, it looks like below. Here I get The erased Image

I want it to be like this enter image description here

how to do it help me

Below is my code:

UIGraphicsBeginImageContext(frontImage.frame.size); [frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)]; CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 50, 50)); CGContextStrokePath(UIGraphicsGetCurrentContext()); frontImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
+2
source share
2 answers

The problem is that you are using a square brush, and you just erase that square at one point in the line that the user drew. Also, the color of your stroke is completely wrong for what you are trying to do.

It looks like you are trying to set the stroke to a clear color and draw a line between the previous and current points. If you want to do this, you must do this:

 CGContextRef currCtx = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor (currCtx, 0, 0, 0, 0); CGContextMoveToPoint (currCtx, startPt.x, startPt.y); CGContextLineToPoint (currCtx, endPt.x, endPt.y); 

In this case, startPt.x / y is the location of the previous touch, and endPt.x / y is the current touch.

Please note that in order for the lines to be as good as in the picture you posted, you will need to use an antialized texture and draw it at each point along the line, changing its size. But the above should give you something workable that looks pretty good.

+1
source
 UIGraphicsBeginImageContext(imageView1.frame.size); [imageView1.image drawInRect:CGRectMake(0, 0,imageView1.frame.size.width, imageView1.frame.size.height)]; CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 25.0); CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]); CGContextBeginPath(UIGraphicsGetCurrentContext()); CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x, point.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point.x, point.y); CGContextStrokePath(UIGraphicsGetCurrentContext()) ; imageView1.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
0
source

All Articles