Capturing Media

The UIImagePickerController is used to get media from the camera or photo library. UIImagePickerController is a modal view controller, meaning you display it with presentViewController:animated:completion: and dismiss it with dismissViewControllerAnimated:completion:.

Similarly to other modal views we’ve seen so far, the UIImagePickerController uses delegation to notify when it’s done and ready for dismissal.

Configuration

The picker is highly customizable, but it’s important to remember that not all devices have all possible capabilities. For example, some older iPads and iPods don’t even have cameras, and some older iPhones only have a back-facing camera. Therefore, it’s important to check for what’s available before proceeding.

isSourceTypeAvailable: checks whether the device supports picking media using the specified source type. Possible source types are:

UIImagePickerControllerSourceTypePhotoLibrary The photo library.
UIImagePickerControllerSourceTypeCamera The built-in camera.
UIImagePickerControllerSourceTypeSavedPhotosAlbum The Camera Roll album, or the photo library if the device does not have a camera.

availableMediaTypesForSourceType: returns the media types available for the selected source type. There are a handful of possible media types, but the two we are concerned with are kUTTypeImage and kUTTypeMovie. These constants are defined in the MobileCoreServices framework, which must therefore be added to the project for us to use them.

On devices with multiple cameras, isCameraDeviceAvailable: returns whether the specified camera is available or not. isFlashAvailableForCameraDevice: can then be used to check whether the specified camera has a flash or not.

The allowsEditing property lets the user edit the media in the picker before it completes.

When capturing video, the videoMaximumDuration property can be used to cap the video length, and the videoQuality property can be used to set the compression type and resolution.

Handling the Result

The media dictionary holds a lot of relevant information:

Key Description
UIImagePickerControllerMediaType The media type, kUTTypeImage or kUTTypeMovie
UIImagePickerControllerOriginalImage The original UIImage.
UIImagePickerControllerEditedImage The edited UIImage, if allowsEditing is set to YES.
UIImagePickerControllerMediaMetadata An NSDictionary holding metadata like EXIF and GPS.
UIImagePickerControllerReferenceURL An NSURL file reference to the original video.
UIImagePickerControllerMediaURL An NSURL file reference to the edited video, if allowsEditing is set to YES.

Comments are closed.