Erasing on UIImageView

后端 未结 3 1954
庸人自扰
庸人自扰 2021-01-31 06:05

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

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 07:02

    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

提交回复
热议问题