When I used the code generated by PaintCode in my app, I get this error: ...invalid context 0x0. This is a serious error...

When I used the code generated by PaintCode in my app, I get this error: ...invalid context 0x0. This is a serious error...

You cannot call the code generated by PaintCode from anywhere. To work properly, the code has to be called from a valid graphics context. Usually, this means that you have to call the code from a drawRect: method of UIView or NSView subclass.

Alternatively, you can also call it from drawInContext: method of CALayer subclass or delegate, but in such case, you must make sure you call the UIGraphicsPushContext(ctx) method before calling the PaintCode-generated code, and UIGraphicsPopContext() after that.

Example of how to use your drawing code in CALayer subclass:

// Swift
override func drawInContext(ctx: CGContext) 
{
		
	UIGraphicsPushContext(ctx)
		StyleKit.drawMyCanvas()
	UIGraphicsPopContext()
}
// Objective-C
- (void)drawInContext: (CGContextRef)ctx
{	
	UIGraphicsPushContext(ctx);
		[StyleKit drawMyCanvas];
	UIGraphicsPopContext();
}

Please, check out Apple's Apple documentation for more information.