IT tutorials
 
Mobile
 

Iphone Application : Working with Rich Media - Using the Photo Library and Camera

12/1/2012 4:55:35 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
The iPhone is great for storing pictures and, with the new high-quality camera in the iPhone 4, great for taking pictures, too. By integrating the photo library with your apps, you can directly access any image stored on the device or take a new picture and use it within your application.

The magic that makes this possible is the UIImagePickerController class. This class will give us a view controller and view where the user can navigate their photos and videos and access the camera. We’ll add this to our main application view as a modal view using the method presentModalViewController within our main MediaPlaygroundViewController instance.

We will need to implement the UIImagePickerControllerDelegate protocol’s imagePickerController:didFinishPickingMediaWithInfo to detect when an image has been chosen and to properly dismiss the image picker.

Implementing the Image Picker

When a user touches the Pick Image button, our application triggers the method chooseImage. Within this method we will allocate the UIImagePickerController, configure the type of media (camera or library) that it will be browsing, set its delegate, and then display it.

Enter the chooseImage method shown in Listing 1.

Listing 1.
 1: -(IBAction)chooseImage:(id)sender {
 2:     UIImagePickerController *imagePicker;
 3:     imagePicker = [[UIImagePickerController alloc] init];
 4:
 5:     if ([toggleCamera isOn]) {
 6:         imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
 7:     } else {
 8:         imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
 9:     }
10:     imagePicker.delegate=self;
11:     [[UIApplication sharedApplication] setStatusBarHidden:YES];
12:     [self presentModalViewController:imagePicker animated:YES];
13:     [imagePicker release];
14: }

					  

In lines 2–3, imagePicker is allocated and initialized as an instance of UIImagePickerController.

Lines 5–9 sets the sourceType property of the image picker to UIImagePickerControllerSourceTypeCamera if the toggleCamera switch is set to on or UIImagePickerControllerSourceTypePhotoLibrary if it isn’t. In other words, the user can use the toggle switch to choose from photo library images or the camera.

Did you Know?

There is another possible source you may want to use: UIImagePickerControllerSourceTypeSavedPhotosAlbum. This is used to view the camera photo roll.


Did you Know?

If you’d like to determine exactly what sort of camera devices are available on your system, you can test using the UIImagePickerController method isCameraDeviceAvailable, which returns a Boolean value:

[UIImagePickerController isCameraDeviceAvailable:<camera type>]

Where the camera type is

UIImagePickerControllerCameraDeviceRear

or

UIImagePickerControllerCameraDeviceFront


Line 10 sets the image picker delegate to be the MediaPlayerViewController class. This means we will need to implement some supporting methods to handle when the user is finished choosing a photo.

In Line 11, we hide the application’s status bar. This is necessary because the photo library and camera interfaces are meant to be viewed fullscreen. First we use [UIApplication sharedApplication] to grab our application object, and then use the application’s setStatusBarHidden method to hide the bar.

Line 12 adds the imagePicker view controller over the top of our existing view.

In line 13, the imagePicker is released. The object, however, will not be released from memory until the modal view that contains it is dismissed.

Displaying the Chosen Image

With what we’ve written so far, the user can now touch the Pick Image button, but not much is going to happen when they navigate to an image. To react to an image selection, we will implement the delegate method imagePickerController: didFinishPickingMediaWithInfo.

This method will automatically be called when the user makes a selection in the image picker. The method is passed an NSDictionary object that can contain several things: the image itself, an edited version of the image (if cropping/scaling is allowed), or information about the image. We must provide the key value to retrieve the value we want. For this application, we will be accessing the object returned by the UIImagePickerControllerOriginalImage key, a UIImage. This UIImage will then be displayed within the chosenImage UIImageView in our interface.

Did you Know?

To learn more about the data that can be returned by the image picker, read the UIImagePickerControllerDelegate Protocol reference within the Apple developer documentation.


Add the imagePickerController:didFinishPickingMediaWithInfo delegate method shown in Listing 2 to the MediaPlaygroundViewController.m file.

Listing 2.
1: - (void)imagePickerController:(UIImagePickerController *)picker
2:         didFinishPickingMediaWithInfo:(NSDictionary *)info {
3:     [[UIApplication sharedApplication] setStatusBarHidden:NO];
4:     [self dismissModalViewControllerAnimated:YES];
5:     chosenImage.image=[info objectForKey:
6:                        UIImagePickerControllerOriginalImage];
7: }

After the image is chosen, we can redisplay the status bar in line 3, and then dismiss the image picker using dismissModalViewControllerAnimated in line 4.

Lines 5–6 do all the remaining work! We access the UIImage that the user has chosen by grabbing the object in the info dictionary that is referenced by the UIImagePickerControllerOriginalImage key. This is assigned to the image property of chosenImage, displaying the image, in all its glory, in the application’s view.

Cleaning Up After the Image Picker

There is still a scenario that must be accounted for before we can call the image-picking portion of our MediaPlayground application complete. A user can click a “cancel” button within the image picker, leaving the picker without choosing anything! The imagePickerControllerDidCancel delegate method was made for exactly this situation. Implement this method to reshow the status bar, and then dismiss the image picker by calling dismissModalViewControllerAnimated: as follows:

- (void)imagePickerControllerDidCancel:
                        (UIImagePickerController *)picker {
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self dismissModalViewControllerAnimated:YES];
}

You should now be able to use the Pick Image button and display photos from the photo library and camera, as shown in Figure 1.

Figure 1. Choose (or capture) and display photos in your application!

 
Others
 
- Iphone Application : Creating and Playing Audio Recordings
- Windows Phone 7 : AppHub and the Windows Marketplace for Mobile (part 2)
- Windows Phone 7 : AppHub and the Windows Marketplace for Mobile (part 1)
- Developing BlackBerry Tablet Applications : Tabbed Application
- Developing BlackBerry Tablet Applications : View-Based Application
- Windows Phone 7 : XNA Game Studio and Framework Overview (part 2) - Implement the Game
- Windows Phone 7 : XNA Game Studio and Framework Overview (part 1) - Hello XNA for Windows Phone
- Windows Phone 7 : Silverlight Tools and Framework Overview
- Iphone Application : Working with Rich Media - Using the Movie Player
- Iphone Application : Exploring Rich Media, Preparing the Media Playground Application
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us