Invalid Content error while drawing a UIBezierPath

I am trying to make a simple circle in viewdidload with the following code:

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100,100) radius:100 startAngle:M_PI/6 endAngle:M_PI clockwise:YES];

    [aPath fill];

I am getting the following error:

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.

I know this problem is already being discussed here but I can not relate. The error occurs at

[aPath fill];

Is this something related to the application lifecycle?


  • The error means that you have no context to draw the path into, and you must have one.

    As this code is in a view controller, you need to decide which context you should be drawing into.

    You can create a context yourself and draw your path into it, which would be beneficial, for example, if you wanted to create an image containing the path. That would be done using CGBitmapContextCreate.

    Or, and probably more what you're looking for, by drawing the path into the view controllers view context. This is available in the drawRect: method. So, you would implement that in your custom view subclass and move your code there.

    An alternative is to use a CAShapeLayer, created with the path (using the CGPath) and add that as a sublayer of the view controllers views layer. Then you don't need to worry about the context at all...