【问题标题】:How can you perform an erase function for a certain point in Quartz2d drawing?如何在 Quartz2d 绘图中对某个点执行擦除功能?
【发布时间】:2011-08-26 22:54:13
【问题描述】:

我用这段代码在quartz2d中画了一条线

CGPoint currentPoint = CGPointMake(rascalImage.center.x, rascalImage.center.y);
        currentPoint.y += 10;


        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawingView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

现在我将如何为橡皮擦相交的地方制作擦除功能?我知道我必须从线上擦除某个点(橡皮擦接触的地方),我只是不知道该怎么做,所以请帮忙!!

【问题讨论】:

    标签: iphone drawing core-graphics quartz-2d erase


    【解决方案1】:
            UIGraphicsBeginImageContext(imgBlankView.frame.size);
            [imgBlankView.image drawInRect:CGRectMake(0, 0, imgBlankView.frame.size.width, imgBlankView.frame.size.height)];
    
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(),lineWidth);
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
            CGContextBeginPath(UIGraphicsGetCurrentContext());  
            CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint1.x, lastPoint1.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            imgBlankView.image = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
    

    【讨论】:

    • 嗨@CrazyDeveloper,我尝试使用您的代码,它可以工作,但是当我执行一系列撤消和重做时,假设我画了一些线然后完全撤消它,然后再次完全重做然后尝试要擦除,它会清除整个矩形,如何解决这个问题,我为此创建了一个不同的帖子,请看stackoverflow.com/questions/11502320/…
    【解决方案2】:

    我假设您不是在纯色背景上绘画?如果你是,只需使用相同的函数在对比线条上绘制背景色线条。

    但是,由于您很可能有一个复杂的背景:一旦您结束图形上下文,并从您绘制的线条中获取 UIImage,就无法仅擦除线条。如果您正在创建某种复杂的绘图应用程序,我建议您使用图层,并简单地将一个图层“堆叠”在另一个之上以创建绘图环境。

    还请查看此帖子: How to erase some portion of a UIImageView's image on iOS?

    【讨论】:

    • 您有机会尝试一下吗?有任何问题,cmets等吗?我回答你的问题了吗?
    • 抱歉回复晚了,我使用 kClearBlendMode 作为混合模式或类似的东西,让它工作
    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2017-11-17
    • 2014-12-24
    相关资源
    最近更新 更多