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

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

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

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


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

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

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

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


这些类型的错误是历史上调用核心图形函数的结果,而不是在一个上下文中建立的drawRect或调用之间,如UIGraphicsBeginImageContext和UIGraphicsEndImageContext(或其他UIKit函数,如开始和结束一个上下文)。

话虽如此,但bilobatum说的没错,这一系列错误可能是他在回答中提到的iOS 7漏洞造成的。如果在你的iOS6目标中没有看到这些错误,或者在快速扫描这个私有框架后,你没有发现任何可疑的核心图形调用,这可能只是iOS 7的错误。接得好,双叶巴坦!


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


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

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(键盘不显示和许多不同的无效上下文错误消息在控制台)。 我只是通过查找SO上的另一个问题找到了一个解决方案:无法找到CFBundle CertUIFramework.axbundle的可执行文件

只做:

点击iOS模拟器>重置内容和设置…然后再跑。

问题不应该再存在了


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


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

    #import <QuartzCore/QuartzCore.h>

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

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

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

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

这样我就得到了错误。

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


我有这个问题在一个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
}

在某些情况下,当您在视图中放置和移动UI元素(特别是以编程方式绘制的自定义元素)时,关闭受影响视图中的自动布局会导致此错误消失。当我发现这个症状时,我正在使用JVFloatLabeledTextField。


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

确定尺寸。宽度或大小。高度不为0,

你可以在CGPostError中添加符号断点来检查


对我来说,答案是我不必要地释放了drawRect:中的图形上下文。在CGPostError上抛出一个符号断点使我找到了罪魁祸首。


得到了我设置的错误

textfield.delegate = self

无需实现任何委托例程。去掉那条线就解决了我的问题


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

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

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


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

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

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

//    CGContextRelease(context);
}

删除版本解决了这个问题


当我在UIActivity子类的activityImage方法中输入错误的图像名称时,我得到了它

- (UIImage *)activityImage
{
    return [UIImage imageNamed:@"img.png"];
}

输入正确的图像为我解决了这个问题。


从模拟器卸载应用程序,并再次运行


我也有同样的问题。 在我的项目中,我尝试创建一个文本字段,并将其添加到我的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不工作。 谢谢那个帮手。


我在自定义CALayer实现的drawInContext(..)方法中得到了这个错误。UIBezierPath尝试使用UIGraphicsGetCurrentContext(),在自定义层中默认为nil。网上的文档解释得很清楚

但是,如果你没有使用UIView对象来绘图,你必须手动使用UIGraphicsPushContext函数将一个有效的上下文推入堆栈。

下面是最终与我的注释内联工作的代码(Swift代码,但解决方案是相同的不管)

override func drawInContext(ctx: CGContext!) {  
    var path = UIBezierPath()

    // 1. Make sure you push the CGContext that was first passed into you.
    UIGraphicsPushContext(ctx)
    path.moveToPoint(CGPoint(x: 0, y: 0))
    path.addLineToPoint(CGPoint(x: 150, y: 150))
    var lineColor = UIColor.blueColor()
    lineColor.setStroke()
    path.lineWidth = 2
    path.stroke(
    // 2. Pop the context after you are done.
    UIGraphicsPopContext()
}

在我的情况下,我有这个警告时,创建一个可靠的图像与caps insets。问题是我没有在“未封顶”区域留下至少1个像素。

    UIImage *image = [UIImage imageNamed:@"name"];
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(20, 10, 20, 10);  //Problem here if the width is 20 or the height is 40
    image = [image resizableImageWithCapInsets:edgeInsets];

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

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


就像其他人在这里评论的一样,当我做以下任何一件事时,我得到了这个警告:

在空白文本框中:双击,触摸并按住,长时间单点。

这似乎只影响iOS 7.0.3

我发现了一个工作周围,警告不会被触发,如果你的纸板不是NULL。

所以我在textFieldDidBeginEditing中做了以下事情:

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

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    if (pasteboard.string.length == 0) {

        pasteboard.string = @" ";

    }
}

我在iphone 4s、5和5s的iOS 7.0.3模拟器中测试了这一点,不再收到警告。我也在iphone 6和6 plus上的iOS 8模拟器中测试了这一点,但我不相信iOS 8会受到影响。

在发现这个工作之前,我经历了两天的挫折,所以我真的希望我的回答能帮助那些有同样问题的人。


我得到了代码错误

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:pointA];
[path addLineToPoint:pointB];
[path addLineToPoint:pointC];
[[UIColor colorWithHexString:@"EFEFEF"] set];
[path fill];
[path closePath];

CAShapeLayer *triangle = [CAShapeLayer layer];
triangle.fillColor = [UIColor colorWithHexString:@"EFEFEF"].CGColor;
triangle.path = path.CGPath;

return triangle;

并在删除代码后

[[UIColor colorWithHexString:@"EFEFEF"] set];
[path fill];

世界变得寂静


如果在使用UIBezierPath并为描边或填充设置颜色时发生错误,请将设置的颜色代码放在drawRect函数中而不是其他地方。


这有点俗气,但对我很管用。

我在UITableViewCell的init中设置了UIImageView,给它一个大小和约束。

然后在我的视图控制器的cellForRowAtIndexPath中我这样做:

let img = PFImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
img.setImage(withFile: nil, defaultText: message.senderUsername)
cell.profileImageView?.image = img.image

这避免了错误,在函数下面,我为UIImageView设置了实际图像。上面的代码是一个很好的默认值