擦除UIImageView

如何擦除前UIImage上的内容,通过滑动手指并显示UIImage的背面UIImage。

我使用了下面的代码- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event擦除前面的图像:

 -(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; } 

我实现了以下输出。

在这里输入图像说明

但是我需要这样的输出。

在这里输入图像说明

我们如何才能实现这个function?

请帮帮我。

尝试这个

 - (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; } 

并使用下面的图像作为橡皮擦,这将完全为你工作。

橡皮擦图像

这段代码在我的最后给了我以下结果: imqage 1

图片2

图片3

图片4

您可以通过混合两张图像来完成此操作

下面有很多简单的方法,你也可以使用GPUImage ..

只需要将两个图像与您想要的输出混合即可。

将两个图像混合在一起

混合更多,你可以尝试

 **kCGBlendModeOverlay,kCGBlendModeSoftLight,kCGBlendModeColorDodge,etc** 

CGBlendMode参数在下面的function..仍然有很多选项有过分两个图像和合并..(甚至与现场相机也可以做到这一点)

 -(UIImage *)blenimagwithimage:(NSString *)imagename mode:(CGBlendMode)kmode with:(float)opeticy { processedimage=nil; UIImage *inputImage; inputImage = [UIImage imageNamed:imagename]; UIGraphicsBeginImageContext(originalImage.size); [originalImage drawInRect:CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height)]; [inputImage drawInRect:CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height) blendMode:kmode alpha:opeticy]; UIImage *layeredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return layeredImage; } 

检查这个示例代码

它提供了这样的function

在这里输入图像说明

在这里输入图像说明