Graphics Reference
In-Depth Information
the entire app feel unresponsive. We can't help that drawing is slow, but it would be good if
we could avoid making the user wait for it to complete.
There are a few ways around this: In some cases, you can speculatively draw view contents
in advance on a separate thread and then set the resultant image directly as the layer
contents . This can be awkward to set up, however, and is only applicable in certain cases.
Core Animation provides a couple of alternatives: CATiledLayer and the
drawsAsynchronously property.
CATiledLayer
We explored CATiledLayer briefly in Chapter 6. In addition to subdividing a layer into
individual tiles that are updated independently (a kind of automation of the dirty rectangles
concept), CATiledLayer also has the interesting feature of calling the
-drawLayer:inContext: method for each tile concurrently on multiple threads. This
avoids blocking the user interface and also enables it to take advantage of multiple
processor cores for faster tile drawing. A CATiledLayer with just a single tile is a cheap
way to implement an asynchronously updating image view.
drawsAsynchronously
In iOS 6, Apple added this intriguing new property to CALayer . The drawsAsynchronously
property modifies the CGContext that is passed to the -drawLayer:inContext: method,
allowing it to defer execution of drawing commands so that they don't block user
interaction.
This isn't the same kind of asynchronous drawing as CATiledLayer uses. The
-drawLayer:inContext: method itself is only ever called on the main thread, but the
CGContext doesn't wait for each drawing command to complete before proceeding.
Instead, it will queue up the commands and perform the actual drawing on a background
thread after the method has returned.
According to Apple, this feature works best for views that are redrawn frequently (such as
our drawing app, or something like a UITableViewCell ) and will not provide any benefit
for layer contents that are only drawn once, or infrequently.
Summary
In this chapter, we discussed the performance challenges around software drawing using
Core Graphics and explored some ways that we can either improve drawing performance or
cut down the amount of drawing we need to do.
In Chapter 14, “Image IO,” we look at image loading performance.
 
Search WWH ::




Custom Search