【问题标题】:CGContextSaveGState invalid contextCGContextSaveGState 无效上下文
【发布时间】:2013-10-15 09:09:11
【问题描述】:

我正在开发一个 iOS 应用程序,它使用实时摄像头来扫描条形码和二维码。但是有时我会出错..这些错误不会使应用程序崩溃,但修复它们可能是个好主意。这些是错误:

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

【问题讨论】:

    标签: iphone ios objective-c xcode


    【解决方案1】:

    UIGraphicsBeginImageContextWithOptions 方法正在使用width = 0 和/或height = 0 传递大小。调试并解决问题。

    【讨论】:

    • 不幸的是,这个解决方案可能会解决这个问题的一些实例,但全部。
    • 天哪,谢谢。对于这样一个简单的问题,这是多么可怕的无用错误消息。
    • 这在操场上也有效。我刚刚在炸毁的线路之前放弃了UIGraphicsBeginImageContextWithOptions(CGSize(width: 500, height: 500), false, 1.0)。 :)
    【解决方案2】:

    如果其他人遇到这个问题,就我而言,我在 CAShapeLayer 的路径上直接错误地调用了 path.stroke()path.fill()。这些方法只应在您实际拥有上下文时调用(即在 draw(rect:) 内部)。

    【讨论】:

    • 我正在使用两个返回 UIBezierPath 的函数对协议进行子类化,并且在返回之前填充路径,而框架应该负责在其上下文中填充。感谢您的提示!
    【解决方案3】:

    我在这样做时遇到此错误

    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];
    

    【讨论】:

    • 是的!在浏览了所有其他线程并尝试了解决方案之后,这是有意义的。直到我用[UIImage imageNamed:@"placeholder"] 更改了在上下文中绘制的现有图像的占位符[UIImage new],才真正出现了不正确的上下文。
    猜你喜欢
    • 2015-12-06
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多