如何对 Quartz2d 绘图中的某个点执行擦除功能?

发布于 2024-12-01 13:04:41 字数 1094 浏览 1 评论 0原文

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

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

I use this code to draw a line in 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;

Now how would I go about making an erase function for just where the eraser intersects? I know that I have to erase a certain point from the line (where the eraser touches), I just don't know how to do this so please help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-12-08 13:04:41
        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();
        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();
弃爱 2024-12-08 13:04:41

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

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

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

I assume you are not drawing on a solid colored background? If you are, just use the same function to draw background-colored lines over the contrasting lines.

But, since you most likely have a complex background: Once you end the graphics context, and get a UIImage from the lines you have drawn, there is no way to erase just the lines. If you are creating some sort of complex drawing app, I would suggest that you use layers, and simply "stack up" layers one on top of the other to create the drawing environment.

Check out this post also:
How to erase some portion of a UIImageView's image on iOS?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文