Ios CGContext无效的上下文0x0

Ios CGContext无效的上下文0x0,ios,uikit,core-graphics,cgcontext,cglayer,Ios,Uikit,Core Graphics,Cgcontext,Cglayer,我正在绘制CGlayers,所以我正在创建层,并用drawRect方法将层绘制到图形上下文中,这样做 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); if(self.currentDrawingLayer == nil) { CGLayerRef layer = CGLayerCreateWithC

我正在绘制CGlayers,所以我正在创建层,并用drawRect方法将层绘制到图形上下文中,这样做

- (void)drawRect:(CGRect)rect
{              
     CGContextRef context = UIGraphicsGetCurrentContext();
     if(self.currentDrawingLayer == nil)
     {
         CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);   
          self.currentDrawingLayer = layer;
     }                  

     CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer);       

}
我在触摸移动功能中执行所有绘图操作

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
      CGContextRef layerContext =  CGLayerGetContext(self.currentDrawingLayer);

      CGContextSetLineCap(layerContext, kCGLineCapRound);
      CGContextSetLineJoin(layerContext, kCGLineJoinRound);
      CGContextSetAllowsAntialiasing(layerContext, YES);
      CGContextSetShouldAntialias(layerContext, YES);
      CGContextSetBlendMode(layerContext,kCGBlendModeClear);
      CGContextSetLineWidth(layerContext, self.eraseWidth);
      CGContextBeginPath(layerContext);
      CGContextAddPath(layerContext, mutablePath);
      CGContextStrokePath(layerContext);
      CGPathRelease(mutablePath);

      [self setNeedsDisplay];
}
但是当我开始画图时,我得到了Touch moved中使用的所有CGContext函数的错误消息,下面我列出了其中一些

<Error>: CGContextBeginPath: 
 This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
<Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
:CGContextBeginPath:
这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
:CGContextAddPath:无效的上下文0x0。这是一个严重的错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的整体降级。此通知是出于礼貌:请解决此问题。这将成为即将进行的更新中的致命错误。
  • 我知道这个错误是因为,层还没有创建,正因为如此,我得到了这个错误,但是层应该在drawRect方法中创建,因为它是图形上下文活动的地方
  • 如果我先调用
    [self-setNeedsDisplay]
    ,然后调用我的绘图函数,最后再调用
    [self-setNeedsDisplay]
    ,那么一切都很好,没有错误,但我不认为,或者更确切地说,不知道这样做是否正确

  • -touchesMoved:withEvent:
    方法中,如果还没有图层,只需返回即可。毕竟,如果用户看不到您的视图,他们就无法绘制。i、 e.添加

     if (!self.currentDrawingLayer)
       return;
    

    在顶部。

    我试过了,绘图变得非常慢,第二次之前的绘图消失了,很好。您正在尝试创建一个兼容层。好的,只需在
    -touchesMoved:withEvent:
    中进行测试,如果
    self.currentDrawingLayer
    为零,则返回。如果您的视图不可见,用户就无法在其上绘制,并且需要在其可见之前进行渲染。因此,您说,让创建在DrawRect和touchesMoved中,我应该只测试条件,对吗?因为层创建代码驻留在DrawRect方法中,在我的情况下,[self-setNeedsDisplay]仅在touchesMoved中调用,用户在任何时候都不能画画,所以无论何时他打开画布并试图写作,他都不会看到任何东西,我不明白,这是怎么回事work@Ranjit当您的视图最初可见时,框架将调用其
    -drawRect:
    方法