How can I erase content on front UIImage by swiping finger on it and display UIImage that is back side of UIView.
I had used following code in - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
to erase front image:
-(void)eraseDrawingContents
{
UIGraphicsBeginImageContext(frontImage.frame.size);
[frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 25.0);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(0, 0), 50, [[UIColor whiteColor]CGColor]);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, lastPoint.x, lastPoint.y);
CGPathAddLineToPoint(path, nil, currentPoint.x, currentPoint.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextAddPath(UIGraphicsGetCurrentContext(), path);
CGContextStrokePath(UIGraphicsGetCurrentContext());
frontImage.image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
I achieved following output.
But I need output like this.
How can we achieve this functionality?
Please help me.
An object that displays a single image or a sequence of animated images in your interface. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.
Any transparency in the image allows the image view’s background to show through. Similarly, any further transparency in the background of the image is dependent on the transparency of the image view and the transparency of the UIImage object it displays.
This is the default value of UIImageView if you initialize it programmatically. scaleAspectFit scales the content to fit within the image view bounds and maintaining the aspect ratio. This might leave an empty space on one axis. This is the default value of UIImageView created in a Storyboard.
Images are an essential component of an app. We use them everywhere, and you might need a different position and size for each place. UIImageView uses its contentMode property to control these behaviors. UIKit offer thirteen different mode for contentMode for us to work with.
Try this
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
self.imgView.image = [self.imgView.image eraseImageAtPoint:location inImageView:self.imgView fromEraser:eraserImg];
}
- (UIImage *)eraseImageAtPoint: (CGPoint)point inImageView: (UIImageView *)imgView fromEraser: (UIImage *)eraser
{
UIGraphicsBeginImageContext(imgView.frame.size);
[self drawInRect:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
[eraser drawAtPoint:point blendMode:kCGBlendModeDestinationOut alpha:1.0];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And use the below image as eraser this will perfectly work for you.
This code is working at my end give me the following result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With