CGContext problems

Posted by Peyman on Stack Overflow See other posts from Stack Overflow or by Peyman
Published on 2010-05-26T04:59:12Z Indexed on 2010/05/26 5:01 UTC
Read the original article Hit count: 365

Filed under:

Hi

I have a CALayer tree hierarchy

A
 B
 C
 D

where A is the view's root layer (and I create and add B,C and D layers. I am using the same delegate method

`- (void) drawLayer:(CALayer *) theLayer inContext:(CGContextRef)context 

to provide content to each of these layers (implemented through a switch statement in the above method). At initiation A's content is drawn (A.contents) sequentially through these methods

   `- (void) drawLayer:(CALayer *) theLayer inContext:(CGContextRef)context 
    -(void) drawCircle:(CALayer *) theLayer inContext:(CGContextRef)context 

where drawCircle does

    CGContextSaveGState(context);

/ draws circle and other paths here / 

    CGContextRestoreGState(context);
    CGImageRef contentImage     = CGBitmapContextCreateImage(context);
    theLayer.contents           = (id) contentImage;
    CGImageRelease(contentImage);

(i.e I save the Context, do the drawing, restore the context, make a bitmap of the context, update the layer's contents, in this case A's, then release the contentimage).

When the user then clicks somewhere in the circle at touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event delegate method I then try to paint the content of B by (still in touchesEnded:)

        CGContextRef context = UIGraphicsGetCurrentContext();
        [self drawLayer:self.B inContext:context];
        [self.B setNeedsDisplay];

the setNeedsDisplay call the delegate - drawLayer:(CALayer *) theLayer inContext:(CGContextRef)context again but this time the switch statement (using a layer flag) calls

[self drawCircle:theLayer inContext:context];

with color red. The problem I am facing is that when -drawCircle: inContext: is called I get a long list of CGContext errors, starting with

<Error>: CGContextSaveGState: invalid context

and ending with

CGBitmapContextCreateImage: invalid context

I played around with making context the view's ivar and it worked so i am sure the context is the problem but I don't know what. I've tried CGContextFlush but it didn't help.

Any help would be appreciated

thank you

© Stack Overflow or respective owner

Related posts about cgcontext