42

I have this problem after I compiled my code with Xcode 7 GM.

According to Apple this is a bug, but it still seems to be an issue. Everything works fine, but is it possible to get rid of these errors?

: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

enter image description here

Someone in other forums said something about status bar, but I don't have any success to eliminate the message. This is not a huge issue, but it's a useless "error". I'm using Interface Builder.

Updated: I used Objective-C if you use Swift, maybe this is the question you're looking for

Community
  • 1
  • 1
Beto
  • 3,438
  • 5
  • 30
  • 37
  • Possible duplicate of [CGContextSaveGState: invalid context 0x0 Error only on device](http://stackoverflow.com/questions/32236204/cgcontextsavegstate-invalid-context-0x0-error-only-on-device) – Sourabh Sharma Oct 15 '15 at 11:03

7 Answers7

57

This also happens for me on 7 GM, but removing UIViewControllerBasedStatusBarAppearance from Info.plist fixed it for me, as said here.

Update: Warning seems to be gone with iOS 9.2

Community
  • 1
  • 1
ernesto
  • 1,771
  • 2
  • 18
  • 18
  • 6
    Hi Ernesto, your solution worked for me too, however, what if I want the UIStatusbar to stay white? Did you find any solution to let the statusbar stay white without those errors? –  Sep 16 '15 at 20:58
  • Hi, I don't know what you mean exactly by 'white', but removing `UIViewControllerBasedStatusBarAppearance` means that the appearance of the status bar is now determined from the visible view controller, you have to implement [`preferredStatusBarStyle`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/preferredStatusBarStyle) there and return the desired value. Note that if your content view controller is embedded in an navigation/tab controller, you have to implement it in the latter. – ernesto Sep 17 '15 at 08:25
  • 1
    That only works if you have auto-layout turned on. If you have it turned off and cannot use UIViewControllerBasedStatusBarAppearance then you pretty much cannot change the status bar text to light. – William T. Sep 23 '15 at 06:01
  • 1
    Then I would just ignore the warning. I think someone in Apple's forum said it doesn't matter. I also see it on my iPhone on some system apps, so it shouldn't be harmful. `Sep 23 15:56:43 iPhone SpringBoard[58] : CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.` ... `Sep 23 15:56:43 iPhone Preferences[223] : CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.` – ernesto Sep 23 '15 at 13:59
  • 1
    [might help](http://stackoverflow.com/a/31887785/3577656) and here is apple [thread](https://forums.developer.apple.com/thread/13683) in which apple staff said,this warning can be avoided for now and it is a known issue. – Rajal Oct 05 '15 at 10:07
  • After removing plist key **UIViewControllerBasedStatusBarAppearance** how i hide status bar? – Shah Nilay Oct 12 '15 at 06:49
  • @NilayShah: Implement `prefersStatusBarHidden` and return `false` similar as [I have described](http://stackoverflow.com/questions/32490837/cgcontextsavegstate-invalid-context-0x0-xcode-7-gm/32499501?noredirect=1#comment53101218_32499501) and in the [Apple Docs](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/prefersStatusBarHidden). – ernesto Oct 12 '15 at 10:36
  • @ernesto `prefersStatusBarHidden' method implements in all UIViewController's. Is there a only one way like .plist file. – Shah Nilay Oct 12 '15 at 11:02
  • @NilayShah No, there isn't. – ernesto Oct 12 '15 at 15:45
9

I'm drawing using UIBezierPath

Removing path.fill() removed the warning.

Ayman Ibrahim
  • 1,359
  • 15
  • 24
  • 2
    This would remove the color fill operation for the bezier path would it not, thus changing the functionality you are trying to achieve? – sweetfa Jan 08 '20 at 11:12
  • @sweetfa I thought so too but in my case, I was building the UIBezierPath only as the basis for a CAShapeLayer. I was using `setStroke()` outside any place like `draw(_ rect` where setting the stroke would actually make sense. – Arjan Mar 16 '22 at 15:47
8

I have found another cause for this error today.

When I wrote some drawing code in my View Controller class, these errors appeared. I realized that I have to create a UIView subclass and do the drawing steps in the override drawRect function. When I moved the drawing code there, all the errors disappeared.

Volomike
  • 23,743
  • 21
  • 113
  • 209
Jiao
  • 101
  • 1
  • 4
  • 1
    Dude you just saved me! Thank you! Just have to say, in Swift 4.0 it's the draw(_ rect: CGRect) that has to be overridden. – Omid Ariyan Jan 08 '18 at 09:28
2

This is a good case to use the debugger. I set a breakpoint in my main and then used the F7 key to step through the code until the warning appeared. Turns out, there's yet another way this bogus error appears:

NSColor *myColor = colorFromRGBA(43,51,59,0.95);
[myColor set];
(self.window).backgroundColor = myColor;

It was generating the warning on [myColor set]. Evidently, I didn't need this and commented it out. At that point, the error went away. Your experience may differ, of course.

Volomike
  • 23,743
  • 21
  • 113
  • 209
  • This was my issue too, except I was using [path stroke] on a UIBezierPath. That's the code that was generating this log for me. – death_au Feb 03 '16 at 22:28
1

UIGraphicsPushContext, UIGraphicsPopContext worked for me

class CustomLayer: CAShapeLayer {
    override func draw(in context: CGContext) {
        UIGraphicsPushContext(context)

        ...Your code here...

        UIGraphicsPopContext()
    }
}
Li Jin
  • 1,879
  • 2
  • 16
  • 23
0

Similar to what Ayman Ibrahim said,

I commented out two lines and it worked like a charm.

    bezier2Path.close()

// color.setFill() // bezier2Path.fill()

Xavier Chia
  • 203
  • 3
  • 5
-7

Restart the device. It fixed my problem.

deepax11
  • 199
  • 3
  • 7