0

I am drawing into CGlayers, So I am creating the Layer and drawing the Layer into graphics context in drawRect method, this way

- (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);       

}

I do all the drawing operations in my touchesMoved function

-(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];
}

But when I start drawing, I get the error messages for all the CGContext functions used in touches moved, of which I am listing a few below.

<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.
  1. I know the error is because, the Layer is not created yet, and because of that, I get this error, but then Layer should be created in drawRect method only because, its where the graphics context is alive.
  2. If I call [self setNeedsDisplay]first and then my drawing functions, and again at last [self setNeedsDisplay], then everything works fine, with no errors, but I dont think, or rather dont know wheteher this is the right thing to do.
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • possible duplicate of [invalid context 0x0 under iOS 7.0 and system degradation](http://stackoverflow.com/questions/19599266/invalid-context-0x0-under-ios-7-0-and-system-degradation) – Abizern Mar 06 '14 at 13:25

1 Answers1

0

In your -touchesMoved:withEvent: method, just return if you don’t have a layer yet. The user can’t draw if they can’t see your view, after all. i.e. add

 if (!self.currentDrawingLayer)
   return;

at the top.

al45tair
  • 4,405
  • 23
  • 30
  • I tried, drawing becomes very slow and secondly previous drawing disappears – Ranjit Mar 06 '14 at 12:51
  • Ah, good point. You’re trying to create a compatible layer. OK, so just put a test in `-touchesMoved:withEvent:` and return if `self.currentDrawingLayer` is nil. The user can’t draw on your view if it isn’t visible, and it needs to render before it will be visible. – al45tair Mar 06 '14 at 12:51
  • So you say, let the creation be in DrawRect and in touchesMoved, I should just test the condition right? – Ranjit Mar 06 '14 at 12:56
  • As layer creation code is residing in drawRect method, and [self setNeedsDisplay] is only called in touchesMoved, in my case, user will not be able to draw, any time, so whenever he opens the canvas and tries to write, he will not see anything, I dont understand, how this will work – Ranjit Mar 06 '14 at 13:21
  • @Ranjit When your view is initially made visible, the framework will call its `-drawRect:` method. – al45tair Mar 06 '14 at 13:23
  • Ok, thanks, I got it, actually my drawRect was called, but as I have few switch cases inside it, the creation of Layer was not getting executed, now its fine – Ranjit Mar 06 '14 at 13:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49152/discussion-between-ranjit-and-alastair) – Ranjit Mar 06 '14 at 13:59