In this recipe, we will
take a look at how we can use the UIImageView
class to display images within a view.
In this recipe, we will start by creating a UIImageView
object that will be used as a container to display our image. For this example, we will be using an image file named Blue-Aqua-Apple.png
.
In order to see how this can be achieved, we need to modify the ViewObjectsExample application that we created in the previous recipe. Perform the following steps to do so:
ViewObjectsExample.xcodeproj
project file.ViewController.xib
file from the project navigator window.Round Rect Button
object we added previously.UIImageView
control so that it will be big enough to hold the image.UIImageView
, and name it imgPicture
.Our next step is to create the code functionality that will be responsible for displaying the image within our imageView
control:
ViewController.m
implementation file from the project navigator.displayImage
method, as shown in the following code snippet:-(void)displayImage { [self.imgPicture setImage:[UIImage imageNamed:@"Blue-Aqua-Apple.png"]]; [self.imgPicture setContentMode:UIViewContentModeScaleAspectFit]; }
viewDidLoad
method:[self displayImage];
When the compilation completes, the iOS Simulator will appear, displaying the image that we just added to our project.
The UIImageView
class is basically a view that has been customized for the purpose of displaying images. We then set the
contentMode
property of the imageView
class to UIViewContentModeScaleAspectFit
, which scales and fills the image to fit the entire contents of UIImageView
.
If you would like to find out more information on the
UIImageView
class, you can refer to the Apple Developer documentation located at http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html.
The contentMode
property accepts an enumeration type named UIViewContentMode
. The following table explains some of these types:
The UIImage
class is the object that represents image-related information. The following table displays some of these file
formats that it currently supports:
File format |
File extension |
---|---|
Portable Network Graphics |
|
Joint Photographic Experts Group |
|
Tagged Image File Format |
|
Graphics Interchange Format |
|
Windows Bitmap Format |
|
Windows Icon Format |
|