A graphics context represents a drawing destination. It contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap, a PDF document, or a printer. You can obtain a graphics context by using Quartz context creation functions or by using higher-level functions provided in the Carbon, Cocoa, or Printing frameworks. Quartz provides creation functions for various flavors of Quartz graphics contexts including bitmap and PDF. The Carbon and Cocoa frameworks provide functions for obtaining window graphics contexts. The Printing framework provides functions that obtain a graphics context appropriate for the kind of destination printer you are using.
This chapter shows you how to create a graphics context for a variety of drawing destinations. A graphics context is represented in your code by the data type CGContextRef, which is an opaque data type. After you obtain a graphics context you can use Quartz 2D functions to draw to the context, perform operations (such as translations) on the context, and change graphics state parameters, such as line width and fill color.
“Creating a Window Graphics Context” shows how to get a context from the Cocoa and Carbon frameworks, what to do if you are moving QuickDraw code to Quartz 2D, and discusses the differences between the HIView and Quartz coordinate systems.
“Creating a PDF Graphics Context” provides code that shows how to obtain and draw into a PDF context.
“Creating a Bitmap Graphics Context” discusses the parameters needed to create a bitmap context, shows how to obtain and draw into one, and introduces one method you can use to draw the resulting image to a window graphics context.
Note: If you want to perform offscreen drawing, before you use a bitmap graphics context, you should first read “CGLayer Drawing.” CGLayer objects, introduced in Mac OS X v10.4, support drawing to layers and provide a much more optimized solution for offscreen drawing than bitmap graphics contexts provide.
“Obtaining a Graphics Context for Printing” describes how to use the Printing framework for Quartz 2D drawing.
Last updated: 2007-12-11