CGContextSaveGState无效上下文

我正在开发一个iOS应用程序,它使用实时相机馈送来扫描条码和QR码。 但是有时我会得到错误..这些错误不会使应用程序崩溃,但是修复它们可能是个好主意。 这些是错误:

<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. <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. <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. <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. <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. <Error>: CGContextDrawImage: 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. <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. <Error>: CGContextSetInterpolationQuality: 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. <Error>: CGContextSetAllowsAntialiasing: 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. <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. <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. <Error>: CGContextFlush: 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. <Error>: CGBitmapContextCreateImage: 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. <Error>: CGContextSetInterpolationQuality: 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. <Error>: CGContextSetAllowsAntialiasing: 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. <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. <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. <Error>: CGContextFlush: 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. <Error>: CGBitmapContextCreateImage: 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. 

我相信这个代码是负责的错误消息:

 - (UIImage *)cropImage:(UIImage *)oldImage sideCrop:(float)crop { CGSize imageSize = oldImage.size; UIGraphicsBeginImageContextWithOptions( CGSizeMake( imageSize.width - 2*crop, imageSize.height ), NO, 0.); [oldImage drawAtPoint:CGPointMake( -crop, 0) blendMode:kCGBlendModeCopy alpha:1.]; UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return croppedImage; } 

UIGraphicsBeginImageContextWithOptions方法正在传递大小与width = 0和/或height = 0 。 debugging并解决问题。

我这样做的时候会得到这个错误

 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) 

所以当边界的宽度或高度为零时。

所以我只是在我的代码中做了一个简单的非零检查

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

我正在使用这个方法改变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]; 

如果其他人遇到这个问题,在我的情况下,我不正确地调用path.stroke()path.fill()直接在CAShapeLayer的path。 这些方法只能在实际上有一个上下文(即内部draw(rect:) )时才被调用。