CGLayer objects (CGLayerRef data type), available in Mac OS X v. 10.4 and later, allow your application to use layers for drawing. CGLayer objects 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. Using a CGLayer, you 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 CGLayer.
Repeated drawing. For example, you might want to create a pattern that consists of the same item drawn over and over. Using a CGLayer, you 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 CGLayer object 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 CGLayer instead of a bitmap graphics context.
CGLayer objects and transparency layers are parallel to CGPath objects and paths created by CGContext functions. In the case of a CGLayer and a CGPath object, you paint to an abstract destination and can then later draw the complete painting to another destination, such as a display or a PDF. When you paint to a transparency layer or use the CGContext functions that draw paths, you draw directly to the destination represented by a graphics context. There is no intermediate, abstract destination for assembling the painting.
This chapter:
Discusses how CGLayer drawing works.
Provides an overview of how to set up and draw to a CGLayer and then draw that layer to a destination.
Gives a specific example that shows how to set up and use two layers for drawing repeated patterns.
How CGLayer Drawing Works
Drawing With a CGLayer
Example: Using Multiple CGLayer objects to Draw a Flag
Last updated: 2007-12-11