invalid context 0x0 under iOS 7.0 and system degra

2019-01-02 19:18发布

I've read as many search results I could find on this dreaded problem, unfortunatelly, each one seems to focus on a specific function call.

My problem is that I get the same error from multiple functions, which I am guessing are being called back from functions that I use.

To make matters worse, the actual code is within a custom private framework which is being imported in another project, and as such, debugging isn't as simple?

Can anyone point me to the right direction? I have a feeling I'm calling certain methods wrongly or with bad context, but the output from xcode is not very helpful at this point.

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

Those errors may occur when a custom view is presented, or one of its inherited classes. At which point they spawn multiple times, until the keyboard won't provide any input. Touch events are still registered, but system slows down, and eventually may lead to unallocated object errors.

EDIT #1: I do have access to the framework being imported, but I do not see anything weird in the classes which causing the issue.

EDIT #2: I just received an email that iOS 7.1 has been released for developers. I'm curious to see if this goes away, or become worse, or can be solved.

26条回答
低头抚发
2楼-- · 2019-01-02 19:48

For me, the answer was that I was unnecessarily releasing the graphics context in drawRect:. Throwing a symbolic breakpoint on CGPostError pointed me to the culprit.

查看更多
骚的不知所云
3楼-- · 2019-01-02 19:49

I had this problem in a UITextField when I held the touch over a blank field with just placeholder text. I used the following work-around to eliminate blank fields:

-(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
}
查看更多
墨雨无痕
4楼-- · 2019-01-02 19:50

In my case I was having this warning when creating a resible image with cap insets. The problem was that I wasn't leaving at least 1 pixel in the "uncapped" area.

    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];
查看更多
与君花间醉酒
5楼-- · 2019-01-02 19:50

I have the same problem. In my project, i have try to create a textField and add it to my pdf file. Old Code:

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

After solved this problem,the code changed :

old:[[UIColor blackColor] setFill]; 

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

I found the solution at UIColor SetFill doesn't work. And Thanks the helper.

查看更多
大哥的爱人
6楼-- · 2019-01-02 19:51

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.

Solutions (if doing custom drawing):

  1. Don't call drawRect: directly, use [view setNeedsDisplay] or if you need immediate drawing use [view.layer draw]
  2. In your drawRect: get the context but don't use it outside the body of this if statement if (context) {<do drawing here>}
查看更多
永恒的永恒
7楼-- · 2019-01-02 19:51

I was creating UIImage from context using below code:

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

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

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

Thus I was getting the error.

So I removed the Derived Data content, restarted my XCode. And it worked.

查看更多
登录 后发表回答