首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除CGContext行

清除CGContext行
EN

Stack Overflow用户
提问于 2013-01-25 22:02:55
回答 3查看 1.4K关注 0票数 1

一旦我在UIImage上画了一些线条(就像画笔应用程序一样),然后我想清除它们,就像橡皮一样。我在这个网站和谷歌上搜索了很多,我尝试了一些代码,但我找不到正确的解决方案。下面是我用来写的代码:

代码语言:javascript
复制
- (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)
    }
}
EN

回答 3

Stack Overflow用户

发布于 2013-01-25 22:57:31

在这种情况下,[UIColor clearColor]将无法工作,请使用CGContextClearRect(context,rect);清除该区域!

票数 2
EN

Stack Overflow用户

发布于 2013-01-25 22:34:44

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

票数 0
EN

Stack Overflow用户

发布于 2013-08-03 19:50:27

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

  • 会记录每个图形(但实际上不会绘制..在DrawRect中我们将绘制..使用“笔划”)
  • 具有粗细、颜色等等...
  • 有效地在视图

上绘制

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

如果你想用其他工具清理你写的路径,(假设你用铅笔工具写成蓝色..和一个橡皮工具清除蓝色线条和使背景重现)你可以使用"containsPoint:“来删除一些点从贝塞尔路径。

我们就是这样做的。

使用bezier路径不是微不足道的,但非常令人兴奋。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14523325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档