An abstract base class for creating graphics renderers.


@interface UIGraphicsRenderer : NSObject


Do not use UIGraphicsRenderer directly. Instead, either use one of the concrete subclasses (UIGraphicsImageRenderer or UIGraphicsPDFRenderer), or create your own subclass.

Graphics renderers provide memory-efficient management of Core Graphics contexts. Core Graphics contexts represent the drawing environment and backing store for 2D Graphics. As you reuse a graphics renderer, it in turn reuses Core Graphics contexts.

Subclassing Notes

You cannot use UIGraphicsRenderer directly, but if the concrete subclasses (UIGraphicsPDFRenderer and UIGraphicsImageRenderer) don't provide the functionality you require, you can create your own subclass.

Consider creating a subclass any time you need to create multiple Core Graphics contexts, each with the same dimensions and attributes, and one of the concrete subclasses (UIGraphicsPDFRenderer or UIGraphicsImageRenderer) doesn't provide the functionality you require.

To create a subclass of UIGraphicsRenderer, first import the appropriate submodule or header, as shown in Listing 1.

Listing 1

Importing the UIGraphicsRenderer subclass module or header

#import <UIKit/UIGraphicsRendererSubclass.h>

A graphics renderer manages a pool of Core Graphics contexts that are reused with repeated uses of the renderer. The renderer creates these CGContextRef objects using the contextWithFormat: class method, and then wraps each of them in an instance of the class returned by the rendererContextClass class method. You must therefore override these two methods in your graphics renderer subclass.

To perform drawing actions on a Core Graphics context, call the runDrawingActions:completionActions:error: method, providing two blocks. Both of these blocks have a UIGraphicsRendererContext argument, providing access to a Core Graphics context.

It is recommended that you create a public method on your renderer subclass that internally wraps the runDrawingActions:completionActions:error: method. This is how the rendering methods operate on the concrete subclasses, for example the imageWithActions: method on UIGraphicsImageRenderer.

Each time the runDrawingActions:completionActions:error: method is called, the renderer calls the prepareCGContext:withRendererContext: method with the CGContextRef and UIGraphicsRendererContext as arguments. Override the prepareCGContext:withRendererContext: method to apply the UIGraphicsRendererContext configuration to the underlying CGContextRef before the renderer invokes the drawing actions.


Initializing a Graphics Renderer

- initWithBounds:

Creates a new graphics renderer with the specified bounds and a default format.

- initWithBounds:format:

Creates a new graphics renderer with the given bounds and format.

Configuring the Renderer


A Boolean value specifying whether the renderer can create output images.


The format used to create the graphics renderer.

Running the Drawing Actions

- runDrawingActions:completionActions:error:

Performs drawing actions on a Core Graphics context prepared by the renderer.

Managing Graphics Contexts

+ contextWithFormat:

Creates a Core Graphics context configured according to the supplied format object.

+ prepareCGContext:withRendererContext:

Applies the configuration specified in the renderer context to the Core Graphics context.

+ rendererContextClass

Specifies the drawing context class used by this graphics renderer.


Inherits From

See Also

Drawing Contexts


The base class for the drawing environments associated with graphics renderers.


A set of drawing attributes that represent the configuration of a graphics renderer context.


A graphics renderer for creating Core Graphics-backed images.


The drawing environment associated with an image renderer.


A set of drawing attributes that represent the configuration of an image renderer context.


A graphics renderer for creating PDFs.


A handler block that you use to draw PDF content.


A drawing environment associated with a PDF renderer.


A set of drawing attributes that represents the configuration of a PDF renderer context.