1

一旦我在 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)
    }
}
4

3 回答 3

2

在这种情况下[UIColor clearColor]不起作用,使用CGContextClearRect(context,rect);清除区域​​!

于 2013-01-25T14:57:31.450 回答
0

用 .填充上下文的所需部分[UIColor clearColor]

于 2013-01-25T14:34:43.600 回答
0

正确的方法是使用 Bezier 路径:

  • 记录每幅画(但实际上不画..在DrawRect中我们将画..使用“stroke”)
  • 有厚度、颜色等...
  • 有效地在视图上绘制

清除它(使用“removeAllPoints”)并调用 setNeedsDisplay 将使背景重新出现。

如果您想使用其他工具清除您编写的路径(例如您使用铅笔工具以蓝色书写.. 以及使用橡胶工具清除蓝线并使背景重新出现)您可以使用“containsPoint:”从贝塞尔路径中删除一些点。

我们是这样做的。

使用贝塞尔路径并非微不足道,但非常令人兴奋.. :)

于 2013-08-03T11:50:26.523 回答