Graphics Programs Reference
In-Depth Information
For the More Curious: Layers, Bitmaps, and Contexts
A layer is simply a bitmap - a chunk of memory that holds the red, green, blue, and alpha
values of each pixel. When you send the message setNeedsDisplay to a UIView in-
stance, that method is forwarded to the view's layer. After the run loop is done processing
an event, every layer marked for re-display prepares a CGContextRef . Drawing routines
called on this context generate pixels that end up in the layer's bitmap.
How do drawing routines get called on the layer's context? After a layer prepares its con-
text, it sends the message drawLayer:inContext: to its delegate. The delegate of an
implicit layer is its view, so in the implementation for drawLayer:inContext: , the
view sends drawRect: to itself. Therefore, when you see this line at the top of your
drawRect: implementations...
- (void)drawRect:(CGRect)r
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
}
you are getting a pointer to the layer's context. All of the drawing in drawRect: is filling
the layer's bitmap, which is then copied to the screen.
Need to see this for yourself? Set an Xcode breakpoint in HypnosisView 's drawRect:
and check out the stack trace in the debug navigator, as shown in Figure 22.8 .
Figure 22.8 Stack trace in drawRect:
A few paragraphs up, we said that the pixels generated by drawing routines “end up in the
layer's bitmap.” What does that mean? When you want to create a bitmap context in Cocoa
 
Search WWH ::




Custom Search