CGContextSaveGState invalid context CGContextSaveGState invalid context ios ios

CGContextSaveGState invalid context

The UIGraphicsBeginImageContextWithOptions method is being passed size with either width = 0 and/or height = 0. Debug and resolve the problem.

In case anyone else is running into this problem, in my case I was incorrectly calling path.stroke() and path.fill() directly on a CAShapeLayer's path. These methods should only be called when you actually have a context (i.e. inside draw(rect:)).

I get this error when doing like

UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size,                                           NO,                                           1.0f);    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *gradientColorImage = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();


(lldb) po gradientLayer.bounds(origin = (x = 0, y = 0), size = (width = 0, height = 0))(origin = (x = 0, y = 0), size = (width = 0, height = 0))(lldb) po gradientLayer.bounds.size(width = 0, height = 0)(width = 0, height = 0)

so when the width or height of the bounds is zero.

SO I just did a simple width non zero check in my code like

+ (UIImage *)gradientColorImageWithColors:(NSArray *)colorsArray gradientType:(GradientImageType)type andSize:(CGSize)imageSize addingLightEffect:(BOOL)addLightEffect {if(imageSize.width==0) {    return nil;}

I was using this method changing the appearance of the UINavigationBar:

[[UINavigationBar appearanceWhenContainedIn: [UINavigationController class], nil] setBackgroundImage: [UIImage gradientColorImageWithColors:@[(id)COLORS_NAVIGATION_BAR_1.CGColor, (id)COLORS_NAVIGATION_BAR_2.CGColor]                                                                                                                          gradientType:GradientImageTypeHorizontal                                                                                                                               andSize:(CGSize){CGRectGetWidth( [UIScreen mainScreen].bounds ), 1.0f}]                                                                                    forBarMetrics:UIBarMetricsDefault];