我正在尝试制作贴纸申请表。 其中一项功能是擦除贴纸。
我能够擦除图像但是如何降低橡皮擦的强度呢?
目前在touchesMoved函数上,
我正在使用此代码:
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:selectedFrame.imageView];
//erase part
UIGraphicsBeginImageContextWithOptions(selectedFrame.imageView.frame.size, NO, 0);
[selectedFrame.imageView.image drawInRect:CGRectMake(0, 0, selectedFrame.imageView.frame.size.width, selectedFrame.imageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 5, 5));
selectedFrame.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
这确实可以正确地擦除图像,但我想降低橡皮擦强度,因此它不会移除所有触摸的图像,但会略微降低图像的不透明度。 附图如下: 当前工作功能输出 - http://cloudart.com.au/projects/appscreenshots/Erased_image.png
所需的功能输出 - http://cloudart.com.au/projects/appscreenshots/app.png
基本上需要管理橡皮擦强度。任何帮助,将不胜感激。
答案 0 :(得分:0)
代码中的2行可以更改您的橡皮擦大小
橡皮擦尺寸设置为5像素,在以下2行中进行任何更改,用给定行中的尺寸替换5。
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5);
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 5, 5));
答案 1 :(得分:0)
感谢您的投入,但最终这对我有用 -
UIGraphicsBeginImageContextWithOptions(selectedFrame.imageView.frame.size, NO, 0);
[selectedFrame.imageView.image drawInRect:CGRectMake(0, 0, selectedFrame.imageView.frame.size.width, selectedFrame.imageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),5);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDestinationOut);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 0.1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
selectedFrame.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 2 :(得分:0)
UIGraphicsBeginImageContext(eraseimg1.frame.size);
[eraseimg1.image drawInRect:CGRectMake(0, 0,eraseimg1.frame.size.width, eraseimg1.frame.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext( ),kCGBlendModeClear);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext( ), 50);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor] CGColor]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastTouch1.x, lastTouch1.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastTouch1.x, lastTouch1.y);
CGContextStrokePath(UIGraphicsGetCurrentContext()) ;
eraseimg1.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();