Graphics Reference
In-Depth Information
It is actually possible to specify a contentsRect with a negative origin or with
dimensions larger than {1, 1}. In this case, the outermost pixels of the image will be
stretched to fill the remaining area.
One of the most interesting applications of contentsRect is that it enables the use of so-
called image sprites. If you've ever done any games programming, you'll be familiar with
the concept of sprites, which are really just images that can be moved around the screen
independently of one another. But outside of the gaming world, the term is usually used to
refer to a common technique for loading sprite images, rather than having anything to do
with movement.
Typically, many sprites will be packed into a single large image that can be loaded in one
go. This carries various benefits over using multiple individual image files in terms of
memory usage, load time, and rendering performance.
Sprites are used in 2D game engines like Cocos2D, which uses OpenGL to display the
images. But we can use sprites in an ordinary UIKit application by leveraging the power of
contentsRect .
To start off with, we need a sprite sheet —a large image containing our smaller sprite
images. Figure 2.7 shows an example sprite sheet.
Figure 2.7 A sprite sheet
Next, we need to load and display these sprites in our app. The principle is quite simple:
We load our large image as normal, assign it as the contents for four separate layers
Search WWH ::




Custom Search