Class

UIGraphicsRenderer

An abstract base class for creating graphics renderers.

Declaration

class UIGraphicsRenderer : NSObject

Overview

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

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.

Topics

Initializing a Graphics Renderer

init(bounds: CGRect)

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

init(bounds: CGRect, format: UIGraphicsRendererFormat)

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

Configuring the Renderer

var allowsImageOutput: Bool

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

var format: UIGraphicsRendererFormat

The format used to create the graphics renderer.

Running the Drawing Actions

Managing Graphics Contexts

class func context(with: UIGraphicsRendererFormat) -> CGContext?

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

class func prepare(CGContext, with: UIGraphicsRendererContext)

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

class func rendererContextClass() -> AnyClass

Specifies the drawing context class used by this graphics renderer.

Relationships

Inherits From

Conforms To

See Also

Drawing Contexts

class UIGraphicsRendererContext

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

class UIGraphicsRendererFormat

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

class UIGraphicsImageRenderer

A graphics renderer for creating Core Graphics-backed images.

class UIGraphicsImageRendererContext

The drawing environment associated with an image renderer.

class UIGraphicsImageRendererFormat

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

class UIGraphicsPDFRenderer

A graphics renderer for creating PDFs.

typealias UIGraphicsPDFRenderer.DrawingActions

A handler block that you use to draw PDF content.

class UIGraphicsPDFRendererContext

A drawing environment associated with a PDF renderer.

class UIGraphicsPDFRendererFormat

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