-1

我使用此代码在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;

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

4

2 回答 2

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();
于 2011-12-08T09:43:41.230 回答
0

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

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

另请查看这篇文章: 如何在 iOS 上擦除 UIImageView 图像的某些部分?

于 2011-08-26T23:41:09.143 回答