Accessing the iPad Camera and Photo Library (Xcode 4)

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Working with Maps on the iPad with MapKit and the MKMapView Class (Xcode 4)An Example iOS 4 iPad Camera and UIImagePickerController Application (Xcode 4)


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book


The iOS 4 SDK provides access to both the iPad camera device and photo library through the UIImagePickerController class. This allows videos and photographs to be taken from within an iPad application and for existing photos and videos to be presented to the user for selection.

This chapter will cover the basics and some of the theory of using the UIImagePickerController class before working through the step by step creation of an example application in An Example iOS 4 iPad Camera and UIImagePickerController Application.




The iOS 4 UIImagePickerController Class

The ultimate purpose of the UIImagePickerController class is to provide applications with either an image or video. It achieves this task by providing the user with access to the camera, camera roll and photo libraries on the device. In the case of the camera, the user is able to either take a photo or record a video depending on the capabilities of the device and the application’s configuration of the UIImagePickerController object. In terms of camera roll and library access, the class provides the application with the existing image or video selected by the user. The controller also allows new photos and videos created within the application to be saved to the library.

Creating and Configuring a UIImagePickerController Instance

In order to use the UIImagePickerController, an instance of the class must first be created. In addition, properties of the instance need to be configured to control the source for the images or videos (camera, camera roll or library). Further, the types of media that are acceptable to the application must also be defined (photos, videos or both). Another configuration option defines whether the user has the option to edit a photo once it has been taken and before it is passed to the application.

The source of the media is defined by setting the sourceType property of the UIImagePickerController object to one of the three supported types:

  • UIImagePickerControllerSourceTypeCamera
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum
  • UIImagePickerControllerSourceTypePhotoLibrary

The types of media acceptable to the application are defined by setting the mediaTypes property, an NSArray object that can be configured to support both video and images. The KUTTypeImage and KUTTypeMove definitions contained in the <MobileCoreServices/MobileCoreServices.h> include file can be used as values when configuring this property. Whether or not the user is permitted to perform editing before the image is passed on to the application is controlled via the allowsEditing boolean property.

The following code creates a UImagePickerController instance and configures it for camera use with movie and image support and editing allowed. It then displays the controller and releases the controller object:

UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];

imagePicker.delegate = self;

imagePicker.sourceType = 
        UIImagePickerControllerSourceTypeCamera;

imagePicker.mediaTypes = [NSArray arrayWithObjects:
        (NSString *) kUTTypeImage,
        (NSString *) kUTTypeMovie, nil];

imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker 
        animated:YES];
[imagePicker release];

It should be noted that the above code also configured the current class as the delegate for the UIImagePickerController instance. This is actually a key part of how the class works and is covered in a later section.


Accessing the iPad Camera Roll and Photo Library

The previous section covered the use of the UIImagePickerController class to provide access to the iPad camera. As previously discussed, this class may also be used to provide applications with access to existing photos and videos stored on the device’s photo library and camera roll. When using the image picker in this context on the iPad the image picker view should be displayed in a popover. This differs considerably from the implementation of UIImagePickerController on iPhone based applications where the photo library is customarily displayed in a view occupying the entire screen. The steps necessary to display the image picker view in a popover are covered in detail in the chapter entitled An Example iOS 4 iPad Camera and UIImagePickerController Application.

Configuring the UIImagePickerController Delegate

When the user is presented with the UIImagePickerController object user interface the application essentially hands control to that object. That being the case, the controller needs some way to notify the application that the user has taken a photo, recorded a video or made a library selection. It does this by calling delegate methods. The class that instantiates a UIImagePickerController instance should, therefore, declare itself as the object’s delegate, conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols and implement the didFinishPickingMediaWithInfo: and imagePickerControllerDidCancel: methods. When the user has selected or created media, the didFinishPickingMediaWithInfo method is called and passed an NSDictionary object containing the media and associated data. In the event that the user cancels the operation the imagePickerControllerDidCancel method is called. In both cases it is the responsibility of the delegate method to dismiss the view controller:

-(void)imagePickerController:
(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     // Code here to work with media
     [self dismissModalViewControllerAnimated:YES];
}

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

The info argument passed to the didFinishPickingMediaWithInfo method is an NSDictionary object containing the data relating to the image or video created or selected by the user. The first step is typically to identify the type of media:

NSString *mediaType = 
  [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
{
	// Media is an image
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
	// Media is a video
}

The original, unedited image selected or photographed by the user may be obtained from the info dictionary as follows:

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

Assuming that editing was enabled on the image picker controller object, the edited version of image may be accessed via the UImagePickerControllerEditedImage dictionary key:

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

If the media is a video, the URL of the recorded media may be accessed as follows:

NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];

Once the image or video URL has been obtained the application can optionally save the media to the library and either display the image to the user or play the video using the MPMoviePlayer class as outlined the chapter entitled Video Playback from within an iOS 4 iPad Application.

Detecting Device Capabilities

Not all iOS devices provide the same functionality. These differences in functionality make it important to detect the capabilities of a device when using the UIImagePickerController class. Fortunately, this may easily be achieved by a call to the isSourceTypeAvailable class method of the UIImagePickerController. For example, to detect the presence of a camera:

if ([UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera])
{
	// code here
}

Similarly, to test for access to the camera roll:

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
	// code here
}

Finally, to check for support for photo libraries:

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypePhotoLibrary])
{
	// code here
}

Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book

Saving Movies and Images

Once a video or photo created by the user using the camera is handed off to the application it is then the responsibility of the application code to save that media into the library. Photos and videos may be saved via calls to the UIImageWriteToSavedPhotosAlbum and UISaveVideoAtPathToSavedPhotosAlbum methods respectively. These methods use a target-action mechanism whereby the save action is initiated and the application continues to run. When the action is complete a specified method is called to notify the application of the success or otherwise of the operation.

To save an image:

UIImageWriteToSavedPhotosAlbum(image, self,
   @selector(image:finishedSavingWithError:contextInfo:),
   nil);

To save a video:

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath))
{
     UISaveVideoAtPathToSavedPhotosAlbum(videoPath,
          self,
          @selector(video:finishedSavingWithError:contextInfo:),
          nil);
}

Finally, the finishedSavingWithError method that will be called when the action is either complete or failed due to an error:

-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)
error contextInfo:(void *)contextInfo
{
    if (error) {
         UIAlertView *alert = [[UIAlertView alloc]
         initWithTitle: @"Save failed"
         message: @"Failed to save image/video"
         delegate: nil
         cancelButtonTitle:@"OK"
         otherButtonTitles:nil];
         [alert show];
         [alert release];
    }
}

Summary

In this chapter we have provided an overview of the UIImagePickerController and looked at how this class can be used either to allow a user to take a picture or record video from within an iPad application or select media from the device photo libraries. Now that the theory has been covered the next chapter entitled An Example iOS 4 iPad Camera and UIImagePickerController Application will work through the development of an example application that implements the theory covered in this chapter.


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book



PreviousTable of ContentsNext
Working with Maps on the iPad with MapKit and the MKMapView Class (Xcode 4)An Example iOS 4 iPad Camera and UIImagePickerController Application (Xcode 4)