【问题标题】:Erase drawing made on image view擦除在图像视图上绘制的绘图
【发布时间】:2023-03-16 04:21:01
【问题描述】:

所以,我正在开发一个虚拟壁画应用程序。

我可以通过在图像视图(其中包含图像)上移动手指来绘制一些随机线。现在我正试图擦除在它上面制作的图纸,但到目前为止还没有成功。

我用谷歌搜索找到了一些解决方案,下面这行是完整的推荐:-

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

使用此绘图可以擦除,但它也会擦除完成绘图的图像。有什么办法可以防止图像被删除?只擦掉上面画的图?

以下是我用来擦除图纸的代码:-

touchesMoved:-

UITouch *touch = [touches anyObject];
CGPoint previousPoint = [touch locationInView:self.upperImageView];
UIGraphicsBeginImageContext(self.upperImageView.frame.size);
[self.upperImageView.image drawInRect:CGRectMake(0, 0, self.upperImageView.frame.size.width, self.upperImageView.frame.size.height)];

CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(),YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), eraserCap);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 45.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.25, 0.25, 0.25, 1.0);
CGMutablePathRef pathB = CGPathCreateMutable();
CGPathMoveToPoint(pathB,nil,location.x,location.y);
CGPathAddLineToPoint(pathB,nil,previousPoint.x,previousPoint.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextAddPath(UIGraphicsGetCurrentContext(),pathB);
CGContextStrokePath(UIGraphicsGetCurrentContext());
upperImageView.image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
location = previousPoint;

touchesEnded:-

UITouch *touch = [touches anyObject];
CGPoint previousPoint = [touch locationInView:self.upperImageView];
UIGraphicsBeginImageContext(self.upperImageView.frame.size);
[self.upperImageView.image drawInRect:CGRectMake(0, 0, self.upperImageView.frame.size.width, self.upperImageView.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), eraserCap);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0,1.0,1.0, 0.0);
CGMutablePathRef pathB = CGPathCreateMutable();
CGPathMoveToPoint(pathB, nil, location.x, location.y);
CGPathAddLineToPoint(pathB, nil, previousPoint.x, previousPoint.y);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextAddPath(UIGraphicsGetCurrentContext(),pathB);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.upperImageView.image = UIGraphicsGetImageFromCurrentImageContext();
// CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
previousPoint = location;

我做错了什么吗?请在这里指导我正确的方向,任何帮助将不胜感激。

【问题讨论】:

  • This 可能会有所帮助。

标签: ios objective-c core-graphics


【解决方案1】:

您可以为图像和绘图使用不同的视图。 或者将绘制的路径保留在数组中,并分别删除每条路径。

【讨论】:

  • 当用户将手指拖到图纸上时,应该会发生擦除..数组在这里有什么帮助?
  • @Shailesh 它不会。这样有助于撤消堆栈。
【解决方案2】:

将所有绘图移动到覆盖UIImageView 的单独UIView。这样,当你去“清除”内容时,原来的UIImageView下面仍然完好无损。

这样的事情会让你做好准备:

- (void)viewWillAppear {
    // Assumes you add a property of type UIView
    self.drawingView = [[UIView alloc] initWithFrame:self.upperImageView.bounds];
    [self.upperImageView addSubview:drawingView];

}

然后,您的绘图代码只需更改为这个(注意您不再需要重新绘制图像):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint previousPoint = [touch locationInView:self.drawingView];
    UIGraphicsBeginImageContext(self.drawingView.frame.size);
    CGContextSaveGState(UIGraphicsGetCurrentContext());
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(),YES);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), eraserCap);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 45.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.25, 0.25, 0.25, 1.0);
    CGMutablePathRef pathB = CGPathCreateMutable();
    CGPathMoveToPoint(pathB,nil,location.x,location.y);
    CGPathAddLineToPoint(pathB,nil,previousPoint.x,previousPoint.y);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextAddPath(UIGraphicsGetCurrentContext(),pathB);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    upperImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRestoreGState(UIGraphicsGetCurrentContext());
    UIGraphicsEndImageContext();
    location = previousPoint;
}

touchesEnded: 方法中进行类似的更改。

【讨论】:

  • 是的..这是一个很好的解决方法!但是由于我正在尝试制作虚拟壁画..我需要“撤消”和“重做”(数组在这里会派上用场)。如果我要维护一个单独的视图,我该如何做一个撤消/重做堆栈?
  • 另外,我还需要实现“颜色填充”功能......就像用户触摸图像上的任何地方并且它为相同颜色的区域着色......我不确定使用覆盖视图是否会有用吗??
  • @Shailesh 无论您是否执行单独的视图,都可以完成撤消/重做堆栈。我在图形方面不够先进,不知道如何进行颜色填充。也许,如果您检查某个特定像素是否已被清除,您将其替换为原始颜色值?我认为你面临着一项艰巨的任务。
猜你喜欢
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 2011-03-27
相关资源
最近更新 更多