tombenner / nui

Style iOS apps with a stylesheet, similar to CSS
MIT License
3.76k stars 461 forks source link

invalid context 0x0 error #153

Open nigelgrange opened 11 years ago

nigelgrange commented 11 years ago

I have a number of errors which get reported on the console when my app starts:

: CGContextSaveGState: invalid context 0x0 : CGContextSetAlpha: invalid context 0x0 : CGContextSaveGState: invalid context 0x0 : CGContextClipToRect: invalid context 0x0 : CGContextTranslateCTM: invalid context 0x0 : CGContextScaleCTM: invalid context 0x0 : CGContextDrawLinearGradient: invalid context 0x0 : CGContextRestoreGState: invalid context 0x0 : CGContextRestoreGState: invalid context 0x0 If I remove NUI from my app (remove the call to [NUISettings init]; ), then these errors disappear. I'm having problems understanding exactly when these calls are made and why the context is invalid - I've only seen these error before when for example drawing to a bitmap context of zero size. I have ignored this error up until now, but with iOS 7 I get reports that this will be a fatal error in an upcoming version, so would like to resolve this now. Thanks for any help you can provide.
nigelgrange commented 11 years ago

Answering my own question... typical :)

I've found this is due to the UINavigationBar frame being zero size, which then gets resized correctly before the view is actually drawn.

Drawing the zero size navigation bar causes the context errors.

I've fixed this by adding:

    if (bar.bounds.size.height == 0) {
        return;
    }

to the top of + (void)renderSizeDependentProperties:(UINavigationBar*)bar

in NUINavigationBarRenderer.m. This works for my app (and style), but I don't know if it is the 'correct' solution.

BenjaminLawson commented 8 years ago

I was getting this same error, and I traced it back to the

+ (UIImage*)caLayerToUIImage:(CALayer*)layer

method in NUIGraphics.m

Apparently my UISegmentedControl had a frame with dimensions (0,0) that was resized properly after the view was loaded, so UIGraphicsBeginImageContextWithOptions was getting initialized to nil because the frame size was 0, thus the "invalid context" error.

Solution: set the frame size to an arbitrary size greater than 0 in viewDidLoad: Swift:

segmentedControl.frame = CGRectMake(frame.origin.x, frame.origin.y, 300.0, 35.0)

As for an official fix, the frame should be checked for invalid size and either give a warning and return or set the frame based on a height/width property.