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

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

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

有人能告诉我正确的方向吗?我有一种感觉,我正在错误地调用某些方法或与坏的上下文,但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已经发布给开发者了。我很好奇这种情况是否会消失,或者变得更糟,或者是否能得到解决。


当前回答

I have had cases where the context returned from UIGraphicsGetCurrentContext() is NULL, and if you try using it for anything this message appears. It is the view's responsibility to push a context using UIGraphicsPushContext prior to calling drawRect:, if you call drawRect: directly instead of [view setNeedsDisplay] you risk the context not being set yet. My hunch is that prior to iOS 7 the context was pushed for the view on init, and now on iOS 7 the context isn't pushed until the first time drawRect: is about to be called. I suspect some UIKit code is calling drawRect: directly and this is why there are issues with some code even when no custom drawing is being done.

解决方案(如果做自定义绘图):

不要直接调用drawRect:,使用[view setNeedsDisplay],或者如果你需要立即绘图,使用[view。层画) 在你的drawRect:获取上下文,但不要在if语句体之外使用if (context) {<do drawing here>}

其他回答

在我的情况下,我在这段代码中有这些错误:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path;

path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(size*2, 0)];
[path addLineToPoint:CGPointMake(size, size*2)];
[path closePath];
[path fill];

shapeLayer.path = path.CGPath;
shapeLayer.strokeColor = [[UIColor blackColor] CGColor];
shapeLayer.fillColor = color;
shapeLayer.lineWidth = width;

[self addSublayer:shapeLayer];

经过一些思考和测试,我发现了问题——这是一个电话:

[path fill];

正如我检测到的-在我的代码中,这个调用是不必要的,因为填充将通过其他方式完成-所以我简单地删除它。

我有这个问题在一个UITextField,当我持有一个空白字段与占位符文本的触摸。我使用以下方法来消除空白字段:

-(void)textFieldDidBeginEditing:(UITextField *)textField{

        textField.text=[@" " stringByAppendingString:textField.text];

        //other stuff here
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{

         if(textField.text.length>0){
             if([[textField.text substringToIndex:1] isEqualToString:@" "])
                 textField.text=[textField.text substringFromIndex:1];
         }
         //  other stuff here
}

如果您想知道是什么代码导致了这些日志,可以在CGPostError上添加一个符号断点。

对我来说,我得到这个错误,因为我正在释放CGContextRef,如下所示:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    // DRAW BACKGROUND CIRCLE
    CGContextSetFillColorWithColor(context, [[UIColor gray] colorWithAlphaComponent:0.6].CGColor);
    CGContextFillEllipseInRect(context, rect);

//    CGContextRelease(context);
}

删除版本解决了这个问题

在某些情况下,你可能需要包含一行#import <QuartzCore/QuartzCore.h>。