如何清除CGContextRef?

Mic*_*ael 1 iphone core-graphics uikit ios 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;

     }

}
Run Code Online (Sandbox Code Playgroud)

Ism*_*ael 5

我假设 DrawingPath 是 CGPath ...您是否清除该 CGPath?这可能是你的问题的原因

在清理道路的行动中,请执行以下操作:

CGPathRelease(drawingPath);
drawingPath = CGPathCreateMutable();
[self setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)

并进行您通常所做的正常绘图。如果路径为空,则不会绘制任何内容