O'Reilly logo

iOS 6 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.2. Taking Photos with the Camera

Problem

You want to ask the user to take a photo with the camera on his iOS device, and you want to access that photo once the user is done.

Solution

Instantiate an object of type UIImagePickerController and present it as a modal view controller on your current view controller. Here is the .h file of the view controller:

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface Taking_Photos_with_the_CameraViewController
           : UIViewController <UINavigationControllerDelegate, 
                               UIImagePickerControllerDelegate>
@end

The delegate of an instance of UIImagePickerController must conform to the UINavigationControllerDelegate and UIImagePickerControllerDelegate protocols. If you forget to include them in the .h file of your delegate object, you’ll get warnings from the compiler when assigning a value to the delegate property of your image picker controller. Please bear in mind that you can still assign an object to the delegate property of an instance of UIImagePickerController where that object does not explicitly conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols, but implements the required methods in these protocols. I, however, suggest that you give a hint to the compiler that the delegate object does, in fact, conform to the aforementioned protocols in order to avoid getting compiler warnings.

In the implementation of the view controller (.m file), we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required