How do I obtain the current graphics context for 2D drawing?

You obtain the current context for 2D drawing from within your implementation of the drawRect: method for the UIView object that you plan to draw to, by calling the function UIGraphicsGetCurrentContext. This returns the current graphics context as an opaque type (CGContextRef).

You can also set the current context by calling the function UIGraphicsPushContext, passing the context you want to set. Then, you can restore a previous context by calling the method UIGraphicsPopContext. For every call to UIGraphicsPushContext, you must provide a balancing UIGraphicsPopContext call.