关于这个可怕的问题,我已经阅读了尽可能多的搜索结果,不幸的是,每个搜索结果似乎都集中在一个特定的函数调用上。

我的问题是,我从多个函数得到相同的错误,我猜是从我使用的函数回调的。

更糟糕的是,实际的代码是在另一个项目中导入的自定义私有框架中,因此,调试不是那么简单吗?

有人能告诉我正确的方向吗?我有一种感觉,我正在错误地调用某些方法或与坏的上下文,但xcode的输出在这一点上不是很有帮助。

: CGContextSetFillColorWithColor: 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. : CGContextSetStrokeColorWithColor: 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. 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. : CGContextSetFlatness: 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. : CGContextAddPath: 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. : CGContextDrawPath: 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. : 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. : CGContextGetBlendMode: 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.

当呈现自定义视图或其继承的类之一时,可能会发生这些错误。这时它们会生成多次,直到键盘不再提供任何输入。触摸事件仍然被注册,但是系统变慢,最终可能导致未分配对象错误。

编辑#1:我确实可以访问正在导入的框架,但我没有看到导致问题的类中有任何奇怪的东西。

编辑#2:我刚刚收到一封电子邮件,iOS 7.1已经发布给开发者了。我很好奇这种情况是否会消失,或者变得更糟,或者是否能得到解决。


当前回答

直接回答: 这个问题是因为你已经使用了核心图形元素,如CGContext等,而不是- (void)drawRect:(CGRect)rect这个方法。

现在请将代码移到该方法。它将停止给你警告/错误。

其他回答

我有同样的问题,我忘记导入QuartzCore/QuartzCore.h,这解决了我的问题与这些错误。

    #import <QuartzCore/QuartzCore.h>

我也有同样的问题。 在我的项目中,我尝试创建一个文本字段,并将其添加到我的pdf文件。 旧的代码:

- (void) drawInContext:(CGContextRef)context {
    //Otherwise we're upside-down
    CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));

    CGContextSetTextDrawingMode(context, kCGTextFill); // This is the default
    [[UIColor blackColor] setFill]; // ***This is the problem ***

    CGFloat x = self.rect.origin.x;
    CGFloat y = self.rect.origin.y + self.font.pointSize;
    [self.text drawAtPoint:CGPointMake(x, y)
            withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:12]}];
}

解决此问题后,代码更改为:

old:[[UIColor blackColor] setFill]; 

new:CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);

我发现解决方案在UIColor SetFill不工作。 谢谢那个帮手。

其他人会要求你在访问核心图形上下文的地方发布代码,但我怀疑这是问题所在。这些无效的上下文0x0错误消息很常见,很容易在iOS 7中重现。事实上,我可以使用零代码的故事板重现错误。我在IB中拖动UITextField到画布上,运行应用程序,双击文本字段内。

在许多情况下,我很难认真对待无效上下文0x0错误消息。我不知道您的情况是否值得更大的关注(我同意Rob Napier的观点,这是值得研究的,特别是如果您显式地使用图形上下文)。

在我自己的项目中,我希望有一天这些错误会神奇地消失(但这一天并没有伴随着7.0.3)。

更新:在安装Xcode 5.1并瞄准iOS 7.1后,我再也不能通过双击空文本字段来重现错误。

我使用下面的代码从上下文创建UIImage:

    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, (CGRect){.size = size});

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

这样我就得到了错误。

所以我删除了派生数据内容,重新启动XCode。这招奏效了。

我得到这个错误是因为我在使用一个UIColor对象作为一个属性在一个NSAttributedString字典,被使用在一个CATextLayer对象。我更改了字典以保存CGColorRef,错误消失了。

[wordAttributes setObject:(id)[UIColor whiteColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName];