UIImage and UIImageView
The system knows how to work with many standard image file types, such as TIFF, JPEG, GIF, and PNG.
In the very simplest case, an image in your app’s bundle can be obtained through the UIImage class method imageNamed:. This method looks for an image file with the supplied name at the top level of your app’s bundle 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:. (You can get a reference to your app’s bundle with [NSBundle mainBundle], and NSBundle then provides instance methods for getting the pathname of a file within the bundle.) There are various other ways of obtaining a UIImage, but these are the most common.
A UIImageView can actually have two images, one assigned to its image property and the other assigned to its highlightedImage property; which is displayed depends on the value of the UIImageView’s highlighted property. This notion of highlighting is purely optional, and you can use it however you like; a UIImageView does not automatically highlight itself.
A UIImageView without an image and without a background color is invisible, so you could start with an empty UIImageView in the place where you will later need an image and assign the image in code as needed. An image may have areas that are transparent, and a UIImageView will respect this; thus an image of any shape can appear, without the user being aware that it resides in a rectangular host.
How a UIImageView draws its image depends upon the setting of its contentMode property. (The contentMode property is inherited from UIView.) For example, UIViewContentModeScaleToFill means the image’s width and height are set to the width and height of the view, thus filling the view completely even if this changes the image’s aspect ratio; UIViewContentModeCenter means the image is drawn centered in the view without altering its size. The best way to get a feel for the meanings of the various contentMode settings is to assign a UIImageView a small image in a nib and then, in the Attributes inspector, change the Mode pop-up menu, and see where and how the image draws itself.
When creating a UIImageView in code, you can take advantage of a convenience initializer, initWithImage: (or initWithImage:highlightedImage:). The default contentMode is UIViewContentModeScaleToFill, but the image is not initially scaled because the view itself is sized to match to the image. You will still probably need to change the UIImageView’s frame, or at least set its center, in order to place it correctly in its superview. In this example, I’ll put a picture of the planet Mars in the center of the window:
UIImageView* iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Mars.png"]];
[self.window addSubview: iv];
iv.center = self.window.center;
[iv release];
On a device with a double-resolution screen (such as the iPhone 4 with Retina display), all these methods of obtaining an image from a file will automatically use, if there is one, a file with the same name extended by @2x, marking it as double-resolution by assigning it a scale property value of 2.0. (So, in this case, we would have a second image file called [email protected].) In this way, your app can contain both a single-resolution and a double-resolution version of an image file; on the double-resolution display device, the double-resolution version of the image is used, and is drawn at the same
size as the single-resolution image. Thus your code continues to work without change, but your images look sharper.
The documentation warns that if a UIImageView is to be assigned multiple images (such as an image and a highlightedImage), they must have the same scale property value. This is because the UIImageView gets its own internal scaling information from an image’s scale at the time it is assigned to it; it does not change its internal scale merely because you switch the value of its highlighted property.
Starting in iOS 4, when an image is obtained by name from the bundle, a file with the same name extended by ~ipad will automatically be used if the app is running on an iPad. You can use this in a universal app to supply different images automatically depending on whether the app
runs on an iPhone or iPod touch, on the one hand, or on an iPad, on the other. This is true not just for images but for any resource obtained by name from the bundle. See Apple’s Resource Programming Guide.