title: Custom CALayer - invalid context 0x0
body: <p>I'm trying to build an app that use layers, my app structure is</p>
  
  <pre><code>UIView --&gt; UIScrollView --&gt; UIView --&gt; LayersView (Custom UIView)
                                     --&gt; UIImageView
  </code></pre>
  
  <p>I want to add multiple layers to the LayersView, so I've build a custom <code>CALayer</code> that use <code>UIBezierPath</code> to draw a set of points.</p>
  
  <p>CALayerBezierPath.h</p>
  
  <pre><code>#import &lt;QuartzCore/QuartzCore.h&gt;
  
  @interface CALayerBezierPath : CALayer {
      NSMutableArray *pointsArray;
  }
  @property (nonatomic, retain) NSMutableArray *pointsArray;
  
  - (void) initVariables;
  - (void) addNewPoints:(CGPoint)newPoint;
  @end
  </code></pre>
  
  <p>CALayerBezierPath.m</p>
  
  <pre><code>#import "CALayerBezierPath.h"
  
  @implementation CALayerBezierPath
  
  @dynamic pointsArray;
  
  -(void)drawInContext:(CGContextRef)ctx {
      NSLog(@"CALayerBezierPath - drawInContext");
      if ([pointsArray count] &gt; 0) {
          UIColor *color = [UIColor redColor];
          [color set];
          UIBezierPath *path = [UIBezierPath bezierPath];
          [path moveToPoint:[[pointsArray objectAtIndex:0] CGPointValue]];
          for (int x = 1; x &lt; [pointsArray count]; x++) {
              [path addLineToPoint:[[pointsArray objectAtIndex:x] CGPointValue]];
          }
          [path closePath]; // Implicitly does a line between p4 and p1
          [path fill]; // If you want it filled, or...
          [path stroke]; // ...if you want to draw the outline.
      }
  }
  
  -(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
      NSLog(@"drawLayer");// It's never called, i don't know why
  }
  
  - (void) initVariables {
      pointsArray = [[NSMutableArray alloc] init];
  }
  
  - (void) addNewPoints:(CGPoint)newPoint {
      [pointsArray addObject:[NSValue valueWithCGPoint:newPoint]];
  }
  
  @end
  </code></pre>
  
  <p>When I'm initiating a new layer in my LayrsView, I'm using this code:</p>
  
  <pre><code>    self.layer.backgroundColor = [UIColor whiteColor].CGColor;
      self.layer.cornerRadius = 20.0;
      self.layer.frame = CGRectInset(self.layer.frame, 0, 0);
  
      for (PlansClass *pclass in layersContent) {
          CALayerBezierPath *sublayer = [CALayerBezierPath layer];
          [sublayer initVariables];
  
          NSDictionary* json = [pclass.listOfPoints objectFromJSONString];
  
          float largerX = 0; float largerY = 0;
          float smallerX = 10000; float smallerY = 10000;
  
          for (NSDictionary *dic in json) {
  
              [sublayer addNewPoints:CGPointMake([[[json objectForKey:dic] objectForKey:@"x"] floatValue], [[[json objectForKey:dic] objectForKey:@"y"] floatValue])];
  
              if (largerX &lt; [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                  largerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
              }
  
              if (smallerX &gt; [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) {
                  smallerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue];
              }
  
              if (largerY &lt; [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                  largerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
              }
  
              if (smallerY &gt; [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) {
                  smallerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue];
              }
          }
          sublayer.frame = CGRectMake(smallerX, smallerY, largerX - smallerX, largerY - smallerY);
          sublayer.backgroundColor = [UIColor redColor].CGColor;
          [self.layer addSublayer:sublayer];
          [sublayer setNeedsDisplay];
      }
  </code></pre>
  
  <p>The problem is whenever the app lunch, it's give me this error:</p>
  
  <pre><code>2012-09-13 08:05:47.648 abcd[20744:707] CALayerBezierPath - drawInContext
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetFillColorWithColor: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetStrokeColorWithColor: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSaveGState: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetFlatness: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextAddPath: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextDrawPath: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextRestoreGState: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSaveGState: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetLineWidth: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetLineJoin: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetLineCap: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetMiterLimit: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextSetFlatness: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextAddPath: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextDrawPath: invalid context 0x0
  Sep 13 08:05:47 ipad-5 abcd[20744] &lt;Error&gt;: CGContextRestoreGState: invalid context 0x0
  </code></pre>
  
  <p>Also, I've tried to <code>NSLog</code> the <code>ctx</code> in the <code>drawInContext</code> methods and it gives me this:</p>
  
  <pre><code>2012-09-13 08:05:47.650 abcd[20744:707] &lt;CGContext 0x15d950&gt;
  </code></pre>
  
  <p>So, Where is the problem, why is the context is invalid, why i can't draw in my custom <code>CALayer</code>?</p>