Graphics Reference
In-Depth Information
2
The Backing Image
A picture is worth a thousand words. An interface is worth a thousand pictures.
Ben Shneiderman
Chapter 1, “The Layer Tree,” introduced the CALayer class and created a simple layer
with a blue background. Background colors are all very well, but layers would be rather
boring if all they could display was a flat color. A CALayer can actually contain a picture
of anything you like. This chapter explores the backing image of CALayer .
The contents Image
CALayer has a property called contents . This property's type is defined as id ,
implying that it can be any kind of object. This is true—in the sense that you can assign any
object you like to the contents property and your app will still compile—however, in
practice, if you supply anything other than a CGImage , then your layer will be blank.
This quirk of the contents property is due to Core Animation's Mac OS heritage. The
reason that contents is defined as an id is so that on Mac OS, you can assign either a
CGImage or an NSImage to the property and it will work automatically. If you try to
assign a UIImage on iOS, however, you'll just get a blank layer. This is a common cause
of confusion for iOS developers who are new to Core Animation.
The headaches don't stop there, though. The type you actually need to supply is a
CGImageRef , which is a pointer to a CGImage struct. UIImage has a CGImage
property that returns the underlying CGImageRef . If you try to assign that to the
CALayer contents property directly, though, it won't compile because CGImageRef
is not really a Cocoa object; it's a Core Foundation type.
 
 
Search WWH ::




Custom Search