Displaying an image within the view

In this recipe, we will take a look at how we can use the UIImageView class to display images within a view.

Getting ready

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.

How to do it...

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:

  1. Open the ViewObjectsExample.xcodeproj project file.
  2. Select the ViewController.xib file from the project navigator window.
  3. From Object Library, drag a UIImageView object onto the subview and place it underneath the Round Rect Button object we added previously.
  4. Resize the UIImageView control so that it will be big enough to hold the image.
  5. Next, create the outlet and property for this UIImageView, and name it imgPicture.
  6. Then, select Files | Add Files to "ViewObjectsExample"…, or alternatively press option + command + A.
  7. Select the Blue-Aqua-Apple.pngimage file to add and then click on the Add button.

Our next step is to create the code functionality that will be responsible for displaying the image within our imageView control:

  1. Open the ViewController.m implementation file from the project navigator.
  2. Next, create the displayImage method, as shown in the following code snippet:
    -(void)displayImage
    {
     [self.imgPicture setImage:[UIImage imageNamed:@"Blue-Aqua-Apple.png"]];
     [self.imgPicture setContentMode:UIViewContentModeScaleAspectFit];
    }
  3. Then, add the following line of code within the viewDidLoad method:
    [self displayImage];
  4. Then, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

When the compilation completes, the iOS Simulator will appear, displaying the image that we just added to our project.

How it works…

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.

Note

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:

Content mode

Description

ScaleToFill

This is the default value. It scales the content to fit the size of the view, changing the aspect ratio as necessary.

ScaleAspectFit

This scales the content to fit the size of the view, while maintaining its aspect ratio. The remaining area of the view becomes transparent.

ScaleAspectFill

This scales the content to fill the size of the view, while maintaining its aspect ratio.

There's more…

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

.png

Joint Photographic Experts Group

.jpg, .jpeg

Tagged Image File Format

.tiff, .tif

Graphics Interchange Format

.gif

Windows Bitmap Format

.bmp

Windows Icon Format

.ico

See also

  • The Adding and customizing views recipe
  • The Selecting images and video from the camera roll recipe in Chapter 7, Multimedia Resources
  • The Capturing media with the camera recipe in Chapter 7, Multimedia Resources
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset