I use a custom background image for my navigation bar. So in my app delegate I set:
UIImage *image1 = [UIImage imageNamed:@"mh.png"];
[image1 drawInRect:CGRectMake(0, 0, 480, 320)];
[[UINavigationBar appearance] setBackgroundImage:image
forBarMetrics:UIBarMetricsDefault];
But this gives me a bunch of errors like
Wed Nov 2 23:05:36 My-MacBook.local MessageDJ[480] <Error>: CGContextScaleCTM: invalid context 0x0
Wed Nov 2 23:05:36 My-MacBook.local MessageDJ[480] <Error>: CGContextDrawImage: invalid context 0x0
Wed Nov 2 23:05:36 My-MacBook.local MessageDJ[480] <Error>: CGContextRestoreGState: invalid context 0x0
Any idea why this happens and how to fix it?
You don’t need the drawInRect method call to set the image, however for completeness sake I will explain why your code is generating errors….
The
UIImagedrawInRectmethod is a UIKit helper method which wraps up some QuartzCore calls to draw an image; it flips the context before drawing so the image appears the correct way up for example. The problem with your code is that there is no graphics context currently set for these underlying calls to work on.First you need a valid graphics context, so you need one of the UI Graphics context calls…
or if you are currently in a drawRect call you can get the current context by using…