There are many reasons your application might need to draw into an offscreen graphics environment. Perhaps the most common is to create a cache containing a complex drawing. Using the cache, the complex drawing can be drawn to the screen quickly many times over. With a rasterization-based drawing library like Quartz 2D, another popular reason is to create a pixel-based representation of a drawing. Quartz 2D includes several techniques for drawing offscreen:
The discussion of creating Quartz metafiles is deferred until Chapter 14, "Working with PDF." This chapter concentrates our attention on the other two techniques on this list. Quartz has always had the ability to create a graphics context capable of drawing into a bitmap. Your application can create a pixel map of its own and ask Quartz 2D to create a graphics context that draws on that pixel map. This technique is very similar to drawing in an offscreen GWorld with QuickDraw or an offscreen DC with GDI. CGLayers are a new concept that Apple has added to Mac OS X 10.4 Tiger. A CGLayer is a fixed-size, offscreen environment for caching a graphic. Redrawing the layer is much more efficient than redrawing the graphic with primitive routines. CGLayer is the preferred offscreen representation for a graphic you plan to cache off and draw to the screen many times. Each of these schemes has advantages and disadvantages. In the sections that follow we do our best to outline the basic techniques and offer the information you need to select the technique that most closely matches your needs. |