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

These sorts of errors are historically the result of calling Core Graphics functions when not within a context that is established within drawRect or between calls like UIGraphicsBeginImageContext and UIGraphicsEndImageContext (or other UIKit functions like that which begin and end a context).

Having said that, though, bilobatum is correct that this particular sequence of errors can be a result of that iOS 7 bug he references in his answer. If not seeing these errors in your iOS6 targets, or if after a quick scan of this private framework you don't find any suspect Core Graphics calls, it may just be this iOS 7 bug. Good catch, bilobatum!

查看更多
宁负流年不负卿
3楼-- · 2019-01-02 19:43

For me I was getting this error because I was releasing the the CGContextRef as shown below:

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

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

//    CGContextRelease(context);
}

Removing the release solved the issue

查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 19:44

Straight Answer : The problem is because you have used Core graphics elements like CGContext etc in other than - (void)drawRect:(CGRect)rect this method.

Now kindly move your code to this method. And it will strop giving you warnings/Errors.

查看更多
梦该遗忘
5楼-- · 2019-01-02 19:44

If the error occurs when you are using UIBezierPath and set color for stroke or fill, put the set color code in drawRect function rather than other places.

查看更多
冷夜・残月
6楼-- · 2019-01-02 19:45

In some cases you may need to include the line #import <QuartzCore/QuartzCore.h>.

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

Turning off autolayout in the affected view causes this error to go away in some cases where you're placing and moving UI elements (especially custom ones which are drawn programmatically) within a view. I was using JVFloatLabeledTextField when I discovered this symptom.

查看更多
登录 后发表回答