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