1

I wrote this simple extension of UIImage to draw an image starting from a predefined color and a size.

This is the code:

extension UIImage {

    class func image(color:UIColor, size:CGSize)->UIImage {

        UIGraphicsBeginImageContextWithOptions(size, true, 2.0);

        let context = UIGraphicsGetCurrentContext()
        let rect = CGRectMake(0, 0, size.width, size.height)

        color.set()

        CGContextFillRect(context, rect)
        var image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }
}

If I launch this code in the ViewDidLoad method of a controller I get this error

Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: 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.
Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: 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.
Oct 31 16:48:02 iPhone Budjet_v2[3387] <Error>: CGContextFillRects: 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.
fatal error: unexpectedly found nil while unwrapping an Optional value

I can't understand what's wrong with my code.

2
  • Your code as posted works fine in a test project. The error is elsewhere. Commented Oct 31, 2014 at 16:48
  • Vague thought: maybe there is no CGContext in existence during viewDidLoad? I'm curious what would happen if you tried it from viewDidAppear; maybe there's no CGContext prior to something actually being drawn. Commented Oct 31, 2014 at 21:33

2 Answers 2

1

This appears to be a bug that was introduced in iOS 7. There is a complete list of the methods that created this error listed in the answer here.

The suggested course of action from this question was to add a symbolic breakpoint to CGPostError to analyze the stack when this error happens in order to determine what is triggering this error.

This error can be triggered by a number of situation and it is impossible to determine which of these scenarios is triggering the error without further research.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1
    if (!(CGSizeEqualToSize(size, CGSizeZero))){

    // your drawing code....

    }else{


return nil;

}

(This ObjC obviously, apologies, I'm not yet an accomplished swift-slinger.... I suspect your problem is a context of size (0,0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.