0417--iOS之UIGraphicsRenderer类

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.

        --不要直接使用UIGraphicsRenderer。相应地,你可以使用UIGraphicsRenderer现有的具体的子类(UIGraphicsImageRenderer or UIGraphicsPDFRenderer),或者自己子类化。

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.

      --你不能直接使用UIGraphicsRenderer,但是如果现有的具体子类(uigraphicsdpfrenderer和UIGraphicsImageRenderer)提供不了你所需的功能,则可以创建自己的子类。

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.

            --在需要创建多个核心图形上下文时考虑创建一个渲染器的子类,而且每个上下文具有相同的维度和属性。(在UIGraphicsPDFRenderer or UIGraphicsImageRenderer这两个子类提供不了你所需要的功能时)

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

         --要创建UIGraphicsRenderer的子类,首先导入适当的子模块或头文件,如清单1所示。

Listing 1

Importing the UIGraphicsRenderer subclass module or header

--导入UIGraphicsRenderer子类模块或头文件

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.

           --图形渲染器管理一个核心图形上下文池,这些上下文在复用渲染器时被复用。渲染器使用它的 context(with:) 类方法创建CGContext对象,然后通过rendererContextClass() 类方法返回上下文的实例。因此,你必须在 子类化图形渲染器的子类中复写这两个方法。

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.

         --要在 Core Graphics context上执行绘图操作,请调用渲染器的runDrawingActions(_:completionActions:)方法,这个方法的参数是两个代码块。这两个代码块都有一个UIGraphicsRendererContext参数,提供了对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.

         --建议您在renderer子类上创建一个公共方法,而且在该方法内部包装runDrawingActions(_:completionActions:) 方法。这就是渲染方法如何操作具体子类的方式,就如UIGraphicsImageRenderer上的image(actions:)方法。

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.

             --每次调用runDrawingActions(_:completionActions:) 方法时,渲染器都会CGContextUIGraphicsRendererContext作为参数调用自己的prepare(_:with:) 方法。在渲染器调用绘图操作之前,重写prepare(_:with:)方法,可以到达为底层的CGContext配置UIGraphicsRendererContext渲染器的目的。

 

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                 --运行绘图操作

func runDrawingActions((UIGraphicsRendererContext) -> Void, completionActions: ((UIGraphicsRendererContext) -> Void)?)

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

typealias UIGraphicsDrawingActions

 

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

NSObject

Conforms To

  • CVarArg

  • Equatable

  • Hashable

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.

你可能感兴趣的:(Drawing,图形绘制相关,IOS,API阅读)