【问题标题】:Proper use of eraser tool above the image while drawing?绘图时正确使用图像上方的橡皮擦工具?
【发布时间】:2025-12-26 13:45:07
【问题描述】:

我有一个 UIView,我有绘制图像。在这个视图中,我正在绘制钢笔工具、矩形工具并更新新图像并进行绘制。就像ACEDrawingView

我在使用橡皮擦工具时遇到了一个问题。当我使用橡皮擦时,背景图像颜色也消失了。请检查屏幕截图。

在上图中,黑色部分是钢笔画,白色部分是我的橡皮擦画。当用户绘制橡皮擦不清除背景图像时,我想要什么。只是绘图。

重绘图像代码

    - (void)updateCacheImage:(BOOL)redraw
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    if (redraw) {
        // erase the previous image
        self.image = nil;

        // load previous image (if returning to screen)
        [[self.prev_image copy] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        // I need to redraw all the lines
        for (id<ACEDrawingTool> tool in self.pathArray) {
            [tool draw];
        }

    } else {
        // set the draw point
        [self.image drawAtPoint:CGPointZero];
        [self.currentTool draw];
    }

    // store the image
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

橡皮擦工具绘图

    - (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddPath(context, self.path);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextSetAlpha(context, self.lineAlpha);
    CGContextStrokePath(context);
}

【问题讨论】:

    标签: ios iphone drawing


    【解决方案1】:

    这是因为您使用的是self.lineColor.CGColor,我猜这是最后选择的颜色,在这种情况下是白色。尝试用[UIColor clearColor]替换当前颜色并添加这行代码CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

    【讨论】:

      【解决方案2】:

      我现在遇到了这个问题......刚刚找到了如下解决方案。您的橡皮擦颜色应该像您的背景视图背景颜色一样。

      示例: CGContextSetStrokeColorWithColor(context, yourviewBackgroundColor);

      【讨论】: