【问题标题】:How to clear a CGContextRef?如何清除 CGContextRef?
【发布时间】:2012-08-28 17:57:24
【问题描述】:

我尝试在 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;

     }

}

【问题讨论】:

    标签: iphone ios uikit core-graphics cgcontextref


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 完美运行...谢谢!
    【解决方案2】:

    清除所有 CGContextRef 绘图:

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, self.bounds); //self.bounds = your drawing base frame
    [self setNeedsDisplay];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多