Meh*_*tri 12 iphone objective-c uiimageview cgcontext ios
如何通过在手指上滑动来擦除前面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;
}
Run Code Online (Sandbox Code Playgroud)
我实现了以下输出.
但我需要像这样的输出.

我们如何实现这一功能?
请帮我.
试试这个
- (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;
}
Run Code Online (Sandbox Code Playgroud)
并使用下面的图像作为橡皮擦,这将完美地为您服务.
这段代码在我的最后工作给我以下结果:



您可以通过混合两个图像来实现此目的
有很多方法,简单的方法如下,您也可以使用 GPUImage ..
只需将两个图像与所需的输出混合即可。
将两个图像相互混合..
混合更多你可以尝试
**kCGBlendModeOverlay,kCGBlendModeSoftLight,kCGBlendModeColorDodge,etc**
Run Code Online (Sandbox Code Playgroud)
在下面函数的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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3068 次 |
| 最近记录: |