4.3 Graphics Contexts

A graphics context is a low-level component of the drawing system representing a destination for drawing commands that will be executed and rendered. Instances of the NSGraphicsContext class represent these contexts. The graphics context also provides an interface to manage graphics states and configure a number of rendering options, such as anti-aliasing, image interpolation, and various settings for drawing paths such as line width and join styles.

At any time in the execution of an application, a current context defines the graphics context for all graphics operations. You can obtain this object using the class method currentContext. Multiple graphics contexts are stored on a stack, so that different parts of an application may configure a context specific to their needs. To push a new context onto the stack, use the method saveGraphicsState. Contexts lower in the stack are restored by invoking restoreGraphicsState, which pulls the current context off the stack, making the next lower one current.

You can use a graphics context to determine whether or not the screen is the current drawing destination. This is useful if you have to handle onscreen and off-screen drawing differently (e.g., to a printer). The isDrawingToScreen method returns YES if drawing is done onscreen, and NO otherwise. The convenience class method currentContextDrawingToScreen does the same thing, saving you the step of first invoking currentContext.



    Part II: API Quick Reference