如何通过在手指上滑动手指来擦除前面UIImage上的内容,并显示UIView背面的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;
}
我实现了以下输出。
但我需要像这样的输出。
我们如何实现此功能?
请帮帮我。
答案 0 :(得分:6)
试试这个
- (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;
}
并使用下面的图像作为橡皮擦,这将非常适合您。
此代码正在我的工作结果中给出以下结果:
答案 1 :(得分:2)
您可以通过混合两个图像来实现此目的
下面有很多简单的方法,你也可以使用GPUImage ..
只需要将两个图像与所需的输出混合。
将两张图片相互混合..
混合更多你可以尝试
**kCGBlendModeOverlay,kCGBlendModeSoftLight,kCGBlendModeColorDodge,etc**
在 CGBlendMode 参数下面的函数..还有很多选项都有超过两个图像并合并..(即使使用直播相机也可以这样做)
-(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;
}
答案 2 :(得分:2)