Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

How CGLayer Drawing Works

A CGLayer, represented by the CGLayerRef data type, is engineered for optimal performance. When possible, Quartz caches a CGLayer object to the video card, which makes drawing the content that’s in a layer much faster than rendering a similar image that’s constructed from a bitmap graphics context. For this reason a CGLayer is typically a better choice for offscreen drawing than a bitmap graphics context is.

All Quartz drawing functions draw to a graphics context. The graphics context provides an abstraction of the destination, freeing you from the details of the destination, such as its resolution. You work in user space, and Quartz performs the necessary transformations to render the drawing correctly to the destination. When you use a CGLayer object for drawing, you also draw to a graphics context. Figure 12-1 illustrates the necessary steps for CGLayer drawing.


Figure 12-2  CGLayer drawing

CGLayer drawing

All CGLayer drawing starts with a graphics context from which you create a CGLayer object using the function CGLayerCreateWithContext. The graphics context used to create a CGLayer object is typically a window graphics context. Quartz creates a CGLayer so that it has all the characteristics of the graphics context—its resolution, colorspace, and graphics state settings. You can provide a size for the layer if you don’t want to use the size of the graphics context. Figure 12-2 shows the graphics context used to create the layer on the left. The gray portion of the box on the right side, labeled CGLayer object, represents the newly created layer.

Before you can draw to the CGLayer, you must obtain the graphics context that’s associated with the CGLayer by calling the function CGLayerGetContext. This graphics context is the same “flavor” as the graphics context used to create the layer. As long as the graphics context used to create the layer is a window graphics context, then the CGLayer graphics context is cached to the GPU if at all possible. The white portion of the box on the right side of Figure 12-2, represents the newly layer graphics context.

You draw to the CGLayer graphics context just as you would draw to any graphics context, passing the layer’s graphic context to the drawing function. Figure 12-2 shows a leaf shape drawn to the layer context.

When you are ready to use the contents of the CGLayer, you can call the functions CGContextDrawLayerInRect or CGContextDrawLayerAtPoint, providing the destination graphics context. Typically you would draw to the same graphics context that you used to create the layer object, but you are not required to. You can draw the layer to any graphics context, keeping in mind that the CGLayer drawing has the characteristics of the graphics context used to create the layer object, which could impose certain constraints (resolution, for example). Figure 12-2 shows the contents of the layer—the leaf—drawn repeatedly to the graphics context used to create the layer object. You can reuse the drawing that’s in a layer as many times as you’d like before releasing the CGLayer object.

Tip:  Use transparency layers when you want to composite parts of a drawing to achieve such effects as shadowing a group of objects. (See “Transparency Layers.”) Use CGLayer objects when you want to draw offscreen or when you need to repeatedly draw the same thing.



< Previous PageNext Page > Hide TOC


Last updated: 2007-12-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice