Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<Error>: CGContextSaveGState: invalid context 0x0 Error

I am getting some issue related with preparing the UIImage out of a UIView. I sometimes (but not always) see the error

<Error>: CGContextSaveGState: invalid context 0x0

I'm using the iPhone SDK 4.0. My code:

-(void)drawRect:(CGRect)rect
{
     // Customized to draw some text
}


-(UIImage*) PrepareBackImage:(CGRect) aRect // aRect = (0,0,1800,1200)
{
    UIImage* background;

    background      = [self GetImageFromView:self toRect:self.frame];
    UIGraphicsBeginImageContext(aRect.size);

    CGPoint backgroundPoint = CGPointMake(0,0);
    [background drawAtPoint:backgroundPoint];

    UIImage* backImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return backImage;
}

- (UIImage *) GetImageFromView:(UIView *)aView toRect:(CGRect)aRect
{
    CGSize pageSize = aRect.size;
    UIGraphicsBeginImageContext(pageSize);
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
like image 641
Sagar... Avatar asked Dec 22 '10 12:12

Sagar...


1 Answers

The Error <Error>: CGContextSaveGState: invalid context 0x0 means either your CGContext is NULL or, the more reasonable explanation in your case, that the size of the Context is empty (CGSizeZero)

like image 121
Jonathan Cichon Avatar answered Oct 06 '22 00:10

Jonathan Cichon