Every iOS drawing operation begins with a context. Conceptually, contexts are very similar to blank pages of paper or empty canvases. They represent an iOS destination for drawing. They contain all the information about the state of the drawing medium—for example, whether the canvas is rotated or transformed in some way, what kind of colors can be drawn onto the canvas, the degree of detail you can paint at any point, and so forth.
In iOS, you primarily work with two kinds of drawing contexts: bitmap contexts and PDF contexts. The Core Image framework offers a third context type, which is used for performing image processing tasks rather than drawing.
Bitmap contexts are essentially rectangular arrays of data. The size of that data depends on the kinds of color each picture element (or “pixel”) represents. Device RGB, as shown in the left image of Figure 1-4, uses three or four bytes per pixel, depending on whether the bitmap is opaque (3 bytes) or not (4 bytes).
An opaque bitmap ignores translucency values, optimizing storage. Translucent images use what’s called an alpha value. This value is stored in a separate byte from the actual color or luminance information. It refers to each pixel’s translucency. The color information for Device RGB is stored in 3 bytes, each corresponding to a single red, green, or blue level.
Device Gray images, as shown on the right in Figure 1-4, use 1 or 2 bytes per pixel. One luminance byte is stored for each pixel and, optionally, one transparency byte.
From a developer’s point of view, PDF contexts work in much the same way as bitmap contexts. You draw to them using identical commands and functions. You set colors and draw shapes and text just as if you were drawing in a view or to an image. There are, however, differences.
PDF contexts contain vector data in their “backing store,” describing the drawing in a resolution-independent manner. Bitmap contexts are rasterized. They use pixel arrays to store the data drawn into them.
PDF contexts also may contain more than one page. You establish a bounding rectangle that specifies the default size and location of each PDF page. An empty rectangle (CGRectZero) defaults to a standard A (letter) page. That is 8.5 by 11 inches, or 612 by 792 points. (Chapter 2 discusses the difference between points and pixels.)
PDF drawings are stored internally as vector-based command sequences. This provides an inherent resolution independence that you don’t see in bitmap drawings. Apple writes in its documentation, “PDF files are resolution independent by nature—the size at which they are drawn can be increased or decreased infinitely without sacrificing image detail. The user-perceived quality of a bitmap image is tied to the resolution at which the bitmap is intended to be viewed.”
You draw into these contexts just like you’d draw into bitmap contexts. The differences primarily lie in their destination (files and data representations) and when you start a new page.
In addition to bitmap and PDF contexts, you may also encounter Core Image contexts.
Core Image Contexts
The Core Image framework helps you process images extremely quickly. It supplies routines that apply digital image processing and computer vision to image sources. With it, you can apply filters, chain filters together, implement feature detection (to find faces and eyes), and analyze images to produce auto-adjustments.
Core Image contexts are image contexts specific to rendering Core Image objects to Quartz 2D and OpenGL. Accelerated by the onboard graphics processing unit (GPU), Core Image contexts integrate with Core Video pixel buffers. Core Image uses its own style of colors (CIColor) and images (CIImage), which have been optimized for Core Image’s fast filtering and image processing features.