CGLayer objects (CGLayerRef
data type) allow your application to use layers for drawing.
Layers are suited for the following:
-
High-quality offscreen rendering of drawing that you plan to reuse. For example, you might be building a scene and plan to reuse the same background. Draw the background scene to a layer and then draw the layer whenever you need it. One added benefit is that you don’t need to know color space or device-dependent information to draw to a layer.
-
Repeated drawing. For example, you might want to create a pattern that consists of the same item drawn over and over. Draw the item to a layer and then repeatedly draw the layer, as shown in Figure 12-1. Any Quartz object that you draw repeatedly—including CGPath, CGShading, and CGPDFPage objects—benefits from improved performance if you draw it to a CGLayer. Note that a layer is not just for onscreen drawing; you can use it for graphics contexts that aren’t screen-oriented, such as a PDF graphics context.
-
Buffering. Although you can use layers for this purpose, you shouldn’t need to because the Quartz Compositor makes buffering on your part unnecessary. If you must draw to a buffer, use a layer instead of a bitmap graphics context.
You need to perform the tasks described in the following section to draw using a CGLayer object:
-
“Create a CGLayer Object Initialized with an Existing Graphics Context”
CGLayerCreateWithContext
-
“Draw to the CGLayer Graphics Context” CGContextFillRect (myLayerContext, myRect)
-
“Draw the Layer to the Destination Graphics Context”
When you are ready to draw the layer to its destination graphics context you can use either of the following functions:
-
CGContextDrawLayerInRect
, which draws a layer to a graphics context in the rectangle specified. -
CGContextDrawLayerAtPoint
, which draws the layer to a graphics context at the point specified.
-