iphone - 如何清除 CGContextRef?

标签 iphone ios uikit core-graphics cgcontextref

我尝试在 ipad 上创建一个简单的手指绘画应用程序。我能够正确地绘制到屏幕的路径,但我希望有一个选项可以完全清除所有绘制路径的屏幕。我目前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有路径都会重新出现在屏幕上。

- (void)drawRect:(CGRect)rect
{
   //Drawing code
   CGContextRef context = UIGraphicsGetCurrentContext();

     if(clearContext == 0){

        CGContextSetLineWidth(context, 6);
        CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
        CGContextAddPath(context, drawingPath);

        CGContextStrokePath(context);
     }
     if(clearContext == 1){

      //This is the code I currently have to clear the context, it is clearly wrong
      //Just used as experimentation.

      CGContextSetBlendMode(context, kCGBlendModeClear);
      CGContextAddPath(context, drawingPath);
      CGContextStrokePath(context);
      clearContext = 0;

     }

}

最佳答案

我假设 drawingPath 是一个 CGPath ...
你在清除 CGPath 吗?这可能是你的问题的原因

在清除路径的操作中,请执行以下操作:

CGPathRelease(drawingPath);
drawingPath = CGPathCreateMutable();
[self setNeedsDisplay];

做你通常做的正常绘图。如果路径为空,则不会绘制任何内容

关于iphone - 如何清除 CGContextRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164854/

相关文章:

iphone - 以编程方式从 UILabel 创建图像

ios - 多个具有 .round lineCap 的 CAShapeLayers 相互重叠

iphone - 语义问题和解析问题 : Missing context for method declaration & expected method body

iphone - 从 iPhone ios 中的 sqlite 数据库获取行数

iphone - objective-c 中的componentsSeparatedByString是什么意思?

iphone - 在 View 上使用drawInRect?

objective-c - 使用 coredata 和多线程枚举时集合发生了变化

ios - 从 Nib 或 Storyboard 在 WKWebView 上设置 WKWebViewConfiguration

ios - 如何使用 [camera takePhoto] 并使用 UIImagePicker 进行编辑 - UIImagePickerController.allowsEditing = YES

swift - CellForRow 被延迟调用