Home Blog Core Graphics errors when calling -[UIImage resizableImageWithCapInsets:]

Core Graphics errors when calling -[UIImage resizableImageWithCapInsets:]

24th October 2013

On a recent iOS project, we noticed a stream of very polite but quite ominous error messages appearing in the application logs when the app was run on a device.

Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextSetBlendMode: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextSetAlpha: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextTranslateCTM: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextScaleCTM: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextGetCTM: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextClipToRect: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextDrawTiledImage: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextGetCTM: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextClipToRect: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: CGContextDrawTiledImage: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.
Oct 23 12:38:34 TMA-1 ResizableImageNullContext[9372] <Error>: 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.

(I thought “This notice is a courtesy…” was a nice touch.)

There were a few things to note about these messages:

  • The errors were only logged when running the app on device – not the simulator.
  • The messages refer to a series of Core Graphics function calls, but without any context as to when or where the errors were happening. Core Graphics is the core drawing framework in iOS and OS X, and is widely used in both Apple and third-party code.
  • The messages were logged consistently when entering one particular view on the app. (That’s a helpful place to start, but there was very little drawing code in that part of the app – and removing it didn’t stop the errors)

The messages indicated that somewhere an attempt was being made to draw into aNULL CGContextRef. It wasn’t our code, and all kinds of searching on the web brought up a small set of results that got us as far as confirming what we’d already figured out – and that wasn’t much.

Eventually, though, we got to this post on the Apple Developer Forums (Apple developer login needed), suggesting a problem with the insets being set on a resizable image.

True enough, that part of the app included a call to -[UIImage resizableImageWithCapInsets:]. This is a very commonly-used API, and we’d had no problems with it before, but we realised that the designer on the project had replaced the base image that was being used with one that was slightly smaller, and the insets were now a bit off.

Some experimenting revealed that if the cap insets in either direction add up to a size equal to or greater than the size of the image to be resized, Core Graphics starts outputting all these error messages.

The experiment can be repeated with a demo project which can be cloned/downloaded from GitHub. If you grab a copy, build it and run on a device and try modifying the insets, then once the insets get too large you’ll see the delightful messages fill up your console. Lovely.

Screenshot of demo application with inappropriately sized insets Screenshot of demo application with inappropriately sized insets

Once you know where to look then this is a straightforward enough problem to deal with, but getting there was a task. We’ve filed a bug with Apple, including this sample project, suggesting that in this case a little more help be given to the developer trying to figure out why Core Graphics is shouting at them so politely.

Related Articles


on 24th October 2013