获取图形上下文 (Obtaining Graphics Contexts)

Obtaining Graphics Contexts 获取图形上下文

Most of the time, graphics contexts are configured for you. Each view object automatically creates a graphics context so that your code can start drawing immediately as soon as your custom drawRect: method is called. As part of this configuration, the underlying UIView class creates a graphics context (a CGContextRef opaque type) for the current drawing environment.

大多数情况下,为您配置图形上下文。 每个视图对象都会自动创建一个图形上下文,以便您的代码可以在调用自定义drawRect:方法后立即开始绘制。 作为此配置的一部分,底层UIView类为当前绘图环境创建图形上下文(CGContextRef opaque类型)。

If you want to draw somewhere other than your view (for example, to capture a series of drawing operations in a PDF or bitmap file), or if you need to call Core Graphics functions that require a context object, you must take additional steps to obtain a graphics context object. The sections below explain how.

如果要绘制视图以外的其他位置(例如,在PDF或位图文件中捕获一系列绘图操作),或者如果需要调用需要上下文对象的Core Graphics函数,则必须执行其他步骤 获取图形上下文对象。 以下部分解释了如何。

For more information about graphics contexts, modifying the graphics state information, and using graphics contexts to create custom content, see Quartz 2D Programming Guide. For a list of functions used in conjunction with graphics contexts, see CGContext Reference, CGBitmapContext Reference, and CGPDFContext Reference.

有关图形上下文,修改图形状态信息以及使用图形上下文创建自定义内容的更多信息,请参阅“Quartz 2D编程指南”。 有关与图形上下文结合使用的函数列表,请参阅CGContext Reference,CGBitmapContext Reference和CGPDFContext Reference。

Drawing to the Screen

If you use Core Graphics functions to draw to a view, either in the drawRect: method or elsewhere, you’ll need a graphics context for drawing. (The first parameter of many of these functions must be a CGContextRef object.) You can call the function UIGraphicsGetCurrentContext to get an explicit version of the same graphics context that’s made implicit in drawRect:. Because it’s the same graphics context, the drawing functions should also make reference to a ULO default coordinate system.

If you want to use Core Graphics functions to draw in a UIKit view, you should use the ULO coordinate system of UIKit for drawing operations. Alternatively, you can apply a flip transform to the CTM and then draw an object in the UIKit view using Core Graphics native LLO coordinate system. Flipping the Default Coordinate System discusses flip transforms in detail.

The UIGraphicsGetCurrentContext function always returns the graphics context currently in effect. For example, if you create a PDF context and then call UIGraphicsGetCurrentContext, you’d receive that PDF context. You must use the graphics context returned by UIGraphicsGetCurrentContext if you use Core Graphics functions to draw to a view.

如果您使用Core Graphics函数绘制到视图,无论是在drawRect:方法还是其他地方,您都需要一个图形上下文来绘制。 (许多这些函数的第一个参数必须是CGContextRef对象。)您可以调用函数UIGraphicsGetCurrentContext来获取drawRect中隐含的相同图形上下文的显式版本。因为它是相同的图形上下文,所以绘图函数也应该引用ULO默认坐标系。

如果要使用Core Graphics函数在UIKit视图中绘制,则应使用UIKit的ULO坐标系进行绘图操作。或者,您可以将翻转变换应用于CTM,然后使用Core Graphics本机LLO坐标系在UIKit视图中绘制对象。翻转默认坐标系详细讨论了翻转变换。

UIGraphicsGetCurrentContext函数始终返回当前有效的图形上下文。例如,如果您创建PDF上下文然后调用UIGraphicsGetCurrentContext,您将收到该PDF上下文。如果使用Core Graphics函数绘制到视图,则必须使用UIGraphicsGetCurrentContext返回的图形上下文。

Note: The UIPrintPageRenderer class declares several methods for drawing printable content. In a manner similar to drawRect:, UIKit installs an implicit graphics context for implementations of these methods. This graphics context establishes a ULO default coordinate system.

**注意:**UIPrintPageRenderer类声明了几种绘制可打印内容的方法。 以类似于drawRect:的方式,UIKit为这些方法的实现安装隐式图形上下文。 此图形上下文建立ULO默认坐标系。

Drawing to Bitmap Contexts and PDF Contexts

UIKit provides functions for rendering images in a bitmap graphics context and for generating PDF content by drawing in a PDF graphics context. Both of these approaches require that you first call a function that creates a graphics context—a bitmap context or a PDF context, respectively. The returned object serves as the current (and implicit) graphics context for subsequent drawing and state-setting calls. When you finish drawing in the context, you call another function to close the context.

Both the bitmap context and the PDF context provided by UIKit establish a ULO default coordinate system. Core Graphics has corresponding functions for rendering in a bitmap graphics context and for drawing in a PDF graphics context. The context that an app directly creates through Core Graphics, however, establishes a LLO default coordinate system.

UIKit提供了在位图图形上下文中渲染图像的功能,以及通过在PDF图形上下文中绘制来生成PDF内容的功能。 这两种方法都要求您首先分别调用创建图形上下文的函数 - 位图上下文或PDF上下文。 返回的对象充当后续绘图和状态设置调用的当前(和隐式)图形上下文。 在上下文中完成绘制后,可以调用另一个函数来关闭上下文。

UIKit提供的位图上下文和PDF上下文都建立了ULO默认坐标系。 Core Graphics具有相应的功能,用于在位图图形上下文中进行渲染以及在PDF图形上下文中进行绘制。 但是,应用程序直接通过Core Graphics创建的上下文建立了LLO默认坐标系。

Note: In iOS, it is recommended that you use the UIKit functions for drawing to bitmap contexts and PDF contexts. However, if you do use the Core Graphics alternatives and intend to display the rendered results, you will have to adjust your code to compensate for the difference in default coordinate systems. See Flipping the Default Coordinate System for more information.

注意:在iOS中,建议您使用UIKit函数绘制到位图上下文和PDF上下文。 但是,如果您确实使用Core Graphics替代方案并打算显示渲染结果,则必须调整代码以补偿默认坐标系中的差异。 有关详细信息,请参阅翻转默认坐标系。

For details, see Drawing and Creating Images (for drawing to bitmap contexts) and Generating PDF Content (for drawing to PDF contexts).

有关详细信息,请参阅绘制和创建图像(用于绘制到位图上下文)和生成PDF内容(用于绘制到PDF上下文)。

猜你喜欢

转载自blog.csdn.net/qfeung/article/details/89423096
今日推荐