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

< Previous PageNext Page > Hide TOC

Drawing With a CGLayer

You need to perform the following tasks to draw using a CGLayer:

  1. “Create a CGLayer Initialized With an Existing Graphics Context”

  2. “Get a Graphics Context for the CGLayer”

  3. “Draw to the CGLayer Graphics Context”

  4. “Draw the CGLayer to the Destination Graphics Context”

The sections that follow describe each task. See “Example: Using Multiple CGLayer objects to Draw a Flag” for a detailed code example.

Create a CGLayer Initialized With an Existing Graphics Context

The function CGLayerCreateWithContext returns a CGLayer that is initialized with an existing graphics context. The layer inherits all the characteristics of the graphics context, including the color space, size, resolution, and pixel format. Later, when you draw the CGLayer to a destination, Quartz automatically color matches the CGLayer to the destination context.

The function CGLayerCreateWithContext takes three parameters:

Get a Graphics Context for the CGLayer

Quartz always draws to a graphics context. Now that you have a CGLayer, you must create a graphics context associated with the layer. Anything you draw into the layer graphics context is part of the CGLayer.

The function CGLayerGetContext takes a CGLayer as a parameter and returns a graphics context associated with the layer.

Draw to the CGLayer Graphics Context

After you obtain the graphics context associated with a CGLayer, you can perform any drawing you’d like to the layer graphics context. You can open a PDF file or an image file and draw the file contents to the layer. You can use any of the Quartz 2D functions to draw rectangles, lines, and other drawing primitives. Figure 12-3 shows an example of drawing rectangles and lines to a layer.

For example, to draw a filled rectangle to a CGLayer graphics context, you call the function CGContextFillRect, supplying the graphics context you obtained from the function CGLayerGetContext. If the graphics context is named myLayerContext, the function call looks like this:

CGContextFillRect (myLayerContext, myRect)


Figure 12-3  A layer that contains two rectangles and a series of lines

A layer that contains two rectangles and a series of lines

Draw the CGLayer 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:

Typically the destination graphics context you supply is a window graphics context and it is the same graphics context you use to create the CGLayer. Figure 12-4 shows the result of repeatedly drawing the CGLayer drawing shown in Figure 12-3. To achieve the patterned effect, you call either of the layer drawing functions repeatedly—CGContextDrawLayerAtPoint or CGContextDrawLayerInRect—changing the offset each time. For example you can call the function CGContextTranslateCTM to change the origin of the coordinate space each time you draw the CGLayer.


Figure 12-4  Drawing a layer repeatedly

Drawing a layer repeatedly

Note:  You are not required to draw a CGLayer to the same graphics context that you use to initialize the CGLayer. However, if you draw the CGLayer to another graphics context, any limitations of the original graphics context are imposed on your drawing.



< 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