Note: This chapter contains information that used to be in iOS Application Programming Guide. This information has not been updated specifically for iOS 4.0.
The UIKit framework provides access to a device’s camera through the UIImagePickerController
class. An instance of this class, called an image picker controller, displays the standard system interface for taking pictures using the camera. You can configure it to display optional controls for resizing and cropping a picture that the user has just taken. In addition, you can use an image picker controller to let the user pick and display photos from their photo library.
If a device doesn’t have a camera available, you cannot display the camera interface. Before attempting to display it, check whether the camera is available by calling the isSourceTypeAvailable:
class method of the UIImagePickerController
class. Always respect this method’s return value. If this method returns NO
, the current device does not have a camera or the camera is currently unavailable for some reason. If the method returns YES
, you can display the picture taking interface as follows:
Create a new UIImagePickerController
object.
Assign a delegate object to the image picker controller.
Typically, you’d use the current view controller as the delegate for the picker, but you can use a different object if you prefer. The delegate object must conform to the UIImagePickerControllerDelegate
and UINavigationControllerDelegate
protocols.
Note: If your delegate does not conform to the UINavigationControllerDelegate
protocol, you may see a warning during compilation. However, because the methods of this protocol are optional, the warning has no impact on your code. To eliminate the warning, include the UINavigationControllerDelegate
protocol in the list of supported protocols for your delegate’s class. You do not need to implement the corresponding methods.
Set the picker type to UIImagePickerControllerSourceTypeCamera
.
Optionally, enable or disable the picture editing controls by assigning an appropriate value to the allowsEditing
property.
Call the presentModalViewController:animated:
method of the currently active view controller, passing a UIImagePickerController
object as the new view controller.
The image picker controller slides the camera interface into position, where it remains active until the user approves the picture or cancels the operation. At that time, the picker controller notifies its delegate of the user’s choice.
Note: Never directly access the view that is managed by an image picker controller. Instead, use the controller’s methods.
Listing 2-1 shows typical code for implementing the preceding set of steps. As soon as you call the presentModalViewController:animated
method, the picker controller takes over, displaying the camera interface and responding to all user interactions until the interface is dismissed. To let the user choose a photo from their photo library (instead of take a picture), change the value in the sourceType
property of the picker to UIImagePickerControllerSourceTypePhotoLibrary
.
Listing 2-1 Displaying the interface for taking pictures
-(BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject |
{ |
if ( (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) |
|| (delegateObject == nil) || (controller == nil)) |
return NO; |
UIImagePickerController* picker = [[UIImagePickerController alloc] init]; |
picker.sourceType = UIImagePickerControllerSourceTypeCamera; |
picker.delegate = delegateObject; |
picker.allowsEditing = YES; |
// Picker is displayed asynchronously. |
[controller presentModalViewController:picker animated:YES]; |
return YES; |
} |
When the user taps the appropriate button to dismiss the camera interface, the UIImagePickerController
notifies the delegate of the user’s choice but does not dismiss the interface. The delegate is responsible for dismissing the picker interface. (Your application is also responsible for releasing the picker when done with it, which you can do in the delegate methods.) It is for this reason that the delegate is actually the view controller object that presented the picker in the first place. Upon receiving the delegate message, the view controller would call its dismissModalViewControllerAnimated:
method to dismiss the camera interface.
Listing 2-2 shows the delegate methods for dismissing the camera interface displayed in Listing 2-1. These methods are implemented by a custom MyViewController
class, which is a subclass of UIViewController
and, for this example, is considered to be the same object that displayed the picker in the first place. The useImage:
method is an empty placeholder for the work you would do in your own version of this class and should be replaced by your own custom code.
Listing 2-2 Delegate methods for the image picker
@implementation MyViewController (ImagePickerDelegateMethods) |
- (void)imagePickerController:(UIImagePickerController *)picker |
didFinishPickingImage:(UIImage *)image |
editingInfo:(NSDictionary *)editingInfo |
{ |
[self useImage:image]; |
// Remove the picker interface and release the picker object. |
[[picker parentViewController] dismissModalViewControllerAnimated:YES]; |
[picker release]; |
} |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker |
{ |
[[picker parentViewController] dismissModalViewControllerAnimated:YES]; |
[picker release]; |
} |
// Implement this method in your code to do something with the image. |
- (void)useImage:(UIImage*)theImage |
{ |
} |
@end |
If image editing is enabled and the user successfully picks an image, the image
parameter of the imagePickerController:didFinishPickingImage:editingInfo:
method contains the edited image. You should treat this image as the selected image, but if you want to store the original image, you can get it (along with the crop rectangle) from the dictionary in the editingInfo
parameter.
Starting in iOS 3.0, you can record video, with included audio, on supported devices. To display the video recording interface, create and push a UIImagePickerController
object, just as for displaying the still-camera interface.
To record video, you must first check that the camera source type (UIImagePickerControllerSourceTypeCamera
) is available and that the movie media type kUTTypeMovie
is available for the camera. Depending on the media types you assign to the mediaTypes
property, the picker can directly display the still camera or the video camera, or a selection interface that lets the user choose.
Using the UIImagePickerControllerDelegate
protocol, register as a delegate of the image picker. Your delegate object receives a completed video recording by way of the imagePickerController:didFinishPickingMediaWithInfo:
method.
On supported devices, you can also pick previously-recorded videos from a user’s photo library.
For more information on using the image picker class, see UIImagePickerController Class Reference. For information on trimming recorded videos, see UIVideoEditorController Class Reference and UIVideoEditorControllerDelegate Protocol Reference.
UIKit provides access to the user’s photo library through the UIImagePickerController
class. This controller displays a photo picker interface, which provides controls for navigating the user’s photo library and selecting an image to return to your application. You also have the option of enabling user editing controls, which let the user the pan and crop the returned image. This class can also be used to present a camera interface.
Because the UIImagePickerController
class is used to display the interface for both the camera and the user’s photo library, the steps for using the class are almost identical for both. The only difference is that you assign the UIImagePickerControllerSourceTypePhotoLibrary
value to the sourceType
property of the picker object. The steps for displaying the camera picker are discussed in “Taking Pictures with the Camera.”
Note: As you do for the camera picker, you should always call the isSourceTypeAvailable:
class method of the UIImagePickerController
class and respect the return value of the method. You should never assume that a given device has a photo library. Even if the device has a library, this method could still return NO
if the library is currently unavailable.
Last updated: 2010-04-30