An abstract base class for creating graphics renderers.
- iOS 10.0+
- Mac Catalyst 13.0+
- tvOS 10.0+
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.
You cannot use
UIGraphics directly, but if the concrete subclasses (
UIGraphics) 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 (
UIGraphics) doesn't provide the functionality you require.
To create a subclass of
UIGraphics, first import the appropriate submodule or header, as shown in Listing 1.
A graphics renderer manages a pool of Core Graphics contexts that are reused with repeated uses of the renderer. The renderer creates these
CGContext objects using the
context(with:) class method, and then wraps each of them in an instance of the class returned by the
renderer 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
run method, providing two blocks. Both of these blocks have a
UIGraphics 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
run method. This is how the rendering methods operate on the concrete subclasses, for example the
image(actions:) method on
Each time the
run method is called, the renderer calls the
prepare(_: method with the
UIGraphics as arguments. Override the
prepare(_: method to apply the
UIGraphics configuration to the underlying
CGContext before the renderer invokes the drawing actions.