UIGraphicsRenderer
An abstract base class for creating graphics renderers.
Declaration
class UIGraphicsRendererOverview
Don’t 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 can’t 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 the following code.
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 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:) 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:) method. This is how the rendering methods operate on the concrete subclasses, for example the image(actions:) method on UIGraphicsImageRenderer.
Each time the runDrawingActions(_:completionActions:) method is called, the renderer calls the prepare(_:with:) method with the CGContext and UIGraphicsRendererContext as arguments. Override the prepare(_:with:) method to apply the UIGraphicsRendererContext configuration to the underlying CGContext before the renderer invokes the drawing actions.