For both functional and aesthetic reasons, images are a pervasive element of app user interfaces. They can be a key differentiating factor for apps.
Many images used by apps, including launch images and app icons, are stored as files in the app’s main bundle. You can have launch images and icons that are specific to device type (iPad versus iPhone and iPod touch) and that are optimized for high-resolution displays. You can find full descriptions of these bundled image files in Advanced App Tricks and App-Related Resources in App Programming Guide for iOS. Updating Your Image Resource Files discusses adjustments that make your image files compatible with high-resolution screens.
In addition, iOS provides support for loading and displaying images using both the UIKit and Core Graphics frameworks. How you determine which classes and functions to use to draw images depends on how you intend to use them. Whenever possible, though, it is recommended that you use the classes of UIKit for representing images in your code. Table C-1 lists some of the usage scenarios and the recommended options for handling them.
Display an image as the content of a view
Display an image as an adornment for part of a view
Load and draw the image using the
Save some bitmap data into an image object
You can do this using the UIKit functions or Core Graphics functions described in Creating New Images Using Bitmap Graphics Contexts.
Save an image as a JPEG or PNG file
System Support for Images
The UIKit framework as well as the lower-level system frameworks of iOS give you a wide range of possibilities for creating, accessing, drawing, writing, and manipulating images.
UIKit Image Classes and Functions
The UIKit framework has three classes and one protocol that are related to images in some way:
Objects of this class represent images in the UIKit framework. You can create them from several different sources, including files and Quartz image objects. Methods of the class enable you to draw images to the current graphics context using different blend modes and opacity values.
UIImageclass automatically handles any required transformations for you, such as applying the proper scale factor (taking into consideration high-resolution displays) and, when given Quartz images, modifying the coordinate system of the image so that it matches the default coordinate system of UIKit (where the y origin is at the upper left).
Objects of this class are views that display either a single image or animate a series of images. If an image is to be the sole content of a view, use the
UIImageViewclass instead of drawing the image.
This class and protocol give your app a way to obtain images (photos) and movies supplied by the user. The class presents and manages user interfaces for choosing and taking photos and movies. When users pick a photo, it delivers the selected
UIImageobject to the delegate, which must implement the protocol methods.
In addition to these classes, UIKit declares functions that you can call to perform a variety of tasks with images:
Drawing into an image-backed graphics context. The
UIGraphicsBeginImageContextfunction creates an offscreen bitmap graphics context. You can draw in this graphics context and then extract a
UIImageobject from it. (See Drawing Images for additional information.)
Getting or caching image data. Each
UIImageobject has a backing Core Graphics image object (
CGImageRef) that you can access directly. You can then pass the Core Graphics object to the Image I/O framework to save the data. You can also convert the image data in a
UIImageobject to either a PNG or JPEG format by calling the
UIImageJPEGRepresentationfunctions. You can then access the bytes in the data object and you can write the image data to a file.
Writing an image to the Photo Album on a device. Call the
UIImageWriteToSavedPhotosAlbumfunction, passing in a
UIImageobject, to put that image in the Photo Album on a device.
Drawing Images identifies scenarios when you would use these UIKit classes and functions.
Other Image-Related Frameworks
You can use several system frameworks other than UIKit to create, access, modify, and write images. If you find that you cannot accomplish a certain image-related task using a UIKit method or function, a function of one of these lower-level frameworks might be able do what you want. Some of these functions might require a Core Graphics image object (
CGImageRef). You can access the
CGImageRef object backing a
UIImage object through the
The Core Graphics framework of Quartz is the most important of the lower-level system frameworks. Several of its functions correspond to UIKit functions and methods; for example, some Core Graphics functions allow you to create and draw to bitmap graphics contexts, while others let you create images from various sources. However, Core Graphics offers more options for handling images. With Core Graphics you can create and apply image masks, create images from portions of existing images, apply color spaces, and access a number of additional image attributes, including bytes per row, bits per pixel, and rendering intent.
The Image I/O framework is closely related to Core Graphics. It allows an app to read and write most image file formats, including the standard web formats, high dynamic range images, and raw camera data. It features fast image encoding and decoding, image metadata, and image caching.
Assets Library is a framework that allows an app to access assets managed by the Photos app. You can get an asset either by representation (for example, PNG or JPEG) or URL. From the representation or URL you can obtain a Core Graphics image object or the raw image data. The framework also lets you write images to the Saved Photos Album.
Supported Image Formats
Table C-2 lists the image formats supported directly by iOS. Of these formats, the PNG format is the one most recommended for use in your apps. Generally, the image formats that UIKit supports are the same formats supported by the Image I/O framework.
Portable Network Graphic (PNG)
Tagged Image File Format (TIFF)
Joint Photographic Experts Group (JPEG)
Graphic Interchange Format (GIF)
Windows Bitmap Format (DIB)
Windows Icon Format
Maintaining Image Quality
Providing high-quality images for your user interface should be a priority in your design. Images provide a reasonably efficient way to display complicated graphics and should be used wherever they are appropriate. When creating images for your app, keep the following guidelines in mind:
Use the PNG format for images. The PNG format provides lossless image content, meaning that saving image data to a PNG format and then reading it back results in the exact same pixel values. PNG also has an optimized storage format designed for faster reading of the image data. It is the preferred image format for iOS.
Create images so that they do not need resizing. If you plan to use an image at a particular size, be sure to create the corresponding image resource at that size. Do not create a larger image and scale it down to fit, because scaling requires additional CPU cycles and requires interpolation. If you need to present an image at variable sizes, include multiple versions of the image at different sizes and scale down from an image that is relatively close to the target size.
Remove alpha channels from opaque PNG files. If every pixel of a PNG image is opaque, removing the alpha channel avoids the need to blend the layers containing that image. This simplifies compositing of the image considerably and improves drawing performance.