Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erasing on UIImageView

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.

enter image description here

But I need output like this.

enter image description here

How can we achieve this functionality?

Please help me.

like image 669
Mehul Mistri Avatar asked Sep 13 '13 10:09

Mehul Mistri


People also ask

What is a UIImageView object?

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.

Why does the background of an image show up in uiimage?

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.

What is the default value of UIImageView scaleaspectfit?

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.

What is contentmode in UIImageView in iOS?

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.


1 Answers

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.

eraser image

This code is working at my end give me the following result:imqage 1

image 2

image 3

image 4

like image 132
Anuj Avatar answered Oct 04 '22 12:10

Anuj