Graphics Reference
In-Depth Information
When working with backing images that are generated programmatically, you'll often need
to remember to manually set the layer
contentsScale
to match the screen scale;
otherwise, your images will appear pixelated on Retina devices. You do so like this:
layer.
contentsScale
= [
UIScreen
mainScreen
].
scale
;
masksToBounds
Now that our snowman is being displayed at the correct size, you might have noticed
something else about him—he's poking outside of the view bounds. By default,
UIView
will happily draw content and subviews outside of its designated bounds. The same is true
for
CALayer
.
There is a property on
UIView
called
clipsToBounds
that can be used to
enable/disable clipping (that is, to control whether a view's contents are allowed to spill out
of their frame).
CALayer
has an equivalent property called
masksToBounds
. By setting
the property to
YES
, we can keep our snowman confined (see Figure 2.5).
Figure 2.5
Using
masksToBounds
to clip the layer contents
contentsRect
The
contentsRect
property of
CALayer
allows us to specify a subrectangle of the
backing image to be displayed inside the layer frame. This allows for much greater
flexibility than the
contentsGravity
property in terms of how the image is cropped
and stretched.