一旦我在UIImage上画了一些线条(就像画笔应用程序一样),然后我想清除它们,就像橡皮一样。我在这个网站和谷歌上搜索了很多,我尝试了一些代码,但我找不到正确的解决方案。下面是我用来写的代码:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(isDrawing == YES) {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:mainImageView.superview];
UIGraphicsBeginImageContext(mainImageView.frame.size);
[mainImageView.image drawInRect:CGRectMake(0, 0, mainImageView.frame.size.width, mainImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);
const CGFloat *components = CGColorGetComponents([color CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
} else {
//Clear current line (like a rubber would do)
}
}发布于 2013-01-25 22:57:31
在这种情况下,[UIColor clearColor]将无法工作,请使用CGContextClearRect(context,rect);清除该区域!
发布于 2013-01-25 22:34:44
使用[UIColor clearColor]填充上下文的所需部分。
发布于 2013-08-03 19:50:27
正确的方法是使用Bezier路径,该路径:
上绘制
清除它(使用"removeAllPoints")并调用setNeedsDisplay将使背景重新出现。
如果你想用其他工具清理你写的路径,(假设你用铅笔工具写成蓝色..和一个橡皮工具清除蓝色线条和使背景重现)你可以使用"containsPoint:“来删除一些点从贝塞尔路径。
我们就是这样做的。
使用bezier路径不是微不足道的,但非常令人兴奋。:)
https://stackoverflow.com/questions/14523325
复制相似问题