UIImage and UIImageView
The basic general UIKit image class is UIImage. UIImage can read a file from disk, so if an image does not need to be created dynamically, but has already been created before your app runs, then drawing may be as simple as providing an image file as a resource in your app’s bundle. The system knows how to work with many standard image file types, such as TIFF, JPEG, GIF, and PNG. You can also obtain image data in some other way, such as by downloading it, and transform this into a UIImage. Conversely, you can draw your own image for display in your interface or for saving to disk (image file output is discussed in Chapter 36).
In the very simplest case, an image file in your app’s bundle can be obtained through the UIImage class method imageNamed:
. This method looks at the top level of your app’s bundle for an image file with the supplied name, including the file extension, and reads it as a UIImage instance. A nice thing about this approach is that memory management is handled for you: the image data may be cached in memory, and if you ask for the same image by calling imageNamed:
again later, the cached data may be supplied immediately. You can also read an image file from anywhere in your app’s bundle using the class method imageWithContentsOfFile:
or the instance method initWithContentsOfFile:
, both of which expect a pathname string; you can get a reference to your app’s bundle with [NSBundle mainBundle]
, and NSBundle then provides instance methods for ...
Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.