4

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?

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • There is not enough info in your question. Have you checked out this one post? http://stackoverflow.com/questions/12424690/uigraphicsgetcurrentcontext-seems-to-return-nil – Anthony Kong Apr 07 '14 at 22:38

1 Answers1

16

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.

spassas
  • 4,778
  • 2
  • 31
  • 39
user3344003
  • 20,574
  • 3
  • 26
  • 62
  • I had the same issue and this was the answer, check if the width AND the height are not 0. – MLBDG May 07 '17 at 18:26