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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
  • Your code as posted works fine in a test project. The error is elsewhere. – Aaron Brager Oct 31 '14 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. – Anna Dickinson Oct 31 '14 at 21:33

2 Answers2

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!

Community
  • 1
  • 1
Janie Larson
  • 504
  • 4
  • 10
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)

Jef
  • 4,728
  • 2
  • 25
  • 33