Search code examples
iosuigraphicscontext

Null Graphics Context


Here is a code excerpt I am using:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^
{

    UIGraphicsBeginImageContextWithOptions(irect.size, YES, 0.0 );
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == 0)
        NSLog (@"Null Graphics Context") ;
    else
        NSLog (@"OK") ;

    . . . .
    // Various drawing functions

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
  }) ;

I am finding that every other call to UIGraphicsGetCurrentContext is returning NULL. I get this message on the console.

CGContextRestoreGState: 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.

What would cause UIGraphicsGetCurrentContext to return an invalid graphic context?


Solution

  • As suggested by the comment, the answer is that the CGRect passed to UIGraphicsBeginImageContextWithOptions has zero area. That apparently cases it to fail to create a graphics context.