Custom CALayer - Invalid 0x0 Context

I am trying to create an application using layers, my application structure

UIView --> UIScrollView --> UIView --> LayersView (Custom UIView) --> UIImageView 

I want to add several layers to LayersView, so I created a custom CALayer that uses UIBezierPath to draw a set of points.

CALayerBezierPath.h

 #import <QuartzCore/QuartzCore.h> @interface CALayerBezierPath : CALayer { NSMutableArray *pointsArray; } @property (nonatomic, retain) NSMutableArray *pointsArray; - (void) initVariables; - (void) addNewPoints:(CGPoint)newPoint; @end 

CALayerBezierPath.m

 #import "CALayerBezierPath.h" @implementation CALayerBezierPath @dynamic pointsArray; -(void)drawInContext:(CGContextRef)ctx { NSLog(@"CALayerBezierPath - drawInContext"); if ([pointsArray count] > 0) { UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:[[pointsArray objectAtIndex:0] CGPointValue]]; for (int x = 1; x < [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 never called, i don't know why } - (void) initVariables { pointsArray = [[NSMutableArray alloc] init]; } - (void) addNewPoints:(CGPoint)newPoint { [pointsArray addObject:[NSValue valueWithCGPoint:newPoint]]; } @end 

When I launch a new layer in my LayrsView, I use this 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 < [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) { largerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue]; } if (smallerX > [[[json objectForKey:dic] objectForKey:@"x"] floatValue]) { smallerX = [[[json objectForKey:dic] objectForKey:@"x"] floatValue]; } if (largerY < [[[json objectForKey:dic] objectForKey:@"y"] floatValue]) { largerY = [[[json objectForKey:dic] objectForKey:@"y"] floatValue]; } if (smallerY > [[[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]; } 

The problem is whenever lunch is in the application, it gives me this error:

 2012-09-13 08:05:47.648 abcd[20744:707] CALayerBezierPath - drawInContext Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFillColorWithColor: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSaveGState: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineWidth: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineJoin: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetLineCap: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetMiterLimit: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextSetFlatness: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextAddPath: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextDrawPath: invalid context 0x0 Sep 13 08:05:47 ipad-5 abcd[20744] <Error>: CGContextRestoreGState: invalid context 0x0 

Also, I tried NSLog ctx in drawInContext methods and this gives me the following:

 2012-09-13 08:05:47.650 abcd[20744:707] <CGContext 0x15d950> 

So where is the problem, why is the context invalid, why can't I draw my custom CALayer ?

+4
source share
1 answer

The problem is that I did not click the ctx context, I tried to click and pop. so the solution is as simple as putting this line in an if statement in drawInContext

 UIGraphicsPushContext(ctx); 

Everything works.

+18
source

Source: https://habr.com/ru/post/1433942/


All Articles