title: CGContextSaveGState: invalid context 0x0 after UIGraphicsBeginImageContextWithOptions
body: <p>Why would this report an invalid context?</p>
  
  <pre><code>&lt;Error&gt;: CGContextSaveGState: 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.
  </code></pre>
  
  <p>This occurs on the call to <code>drawInRect</code> in a category method on UIImage:</p>
  
  <pre><code>- (UIImage *)myImagePaddedToSize:(CGSize)newSize {
      UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
      CGSize oldSize = self.size;
  
      CGRect rect = (CGRect){
          {
              floorf((newSize.width-oldSize.width)/2),
              floorf((newSize.height-oldSize.height)/2)
          },
          oldSize
      };
      [self drawInRect:rect];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
  }
  </code></pre>
  
  <p>My understanding is that <code>UIGraphicsBeginImageContextWithOptions</code> establishes a context that I draw in. And this seems to usually be the care, but very rarely I get this failure instead.</p>
  
  <p>This is with the iOS 7 simulator included with Xcode 5.1.1.</p>