CGContextSaveGState context incorrect

Je développe une application iOS qui utilise un stream de camera en direct pour scanner les codes barre et qr. Cependant, parfois, je reçois des erreurs .. ces erreurs ne plantent pas l'application, mais ce pourrait être une bonne idée de les corriger. Ce sont les erreurs:

<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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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 consortingbuting 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. 

Et je crois que ce code est responsable du message d'erreurs:

 - (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; } 

La méthode UIGraphicsBeginImageContextWithOptions est passée en taille avec width = 0 et / ou height = 0 . Déboguer et résoudre le problème.

Je reçois cette erreur en faisant comme

 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) 

donc quand la largeur ou la hauteur des bornes est nulle.

Donc je viens de faire une simple vérification de largeur non nulle dans mon code comme

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

J'utilisais cette méthode en changeant l'apparence de l'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}] forBarMesortingcs:UIBarMesortingcsDefault]; 

Au cas où quelqu'un d'autre rencontrerait ce problème, dans mon cas, path.stroke() et path.fill() directement sur le path d'un CAShapeLayer. Ces methods ne devraient être appelées que lorsque vous avez réellement un context (ie dedans draw(rect:) ).