O'Reilly logo

iOS 5 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

11.6. Retrieving Photos and Videos From the Photo Library

Problem

You want users to be able to pick a photo or a video from their photo library and use it in your application.

Solution

Use the UIImagePickerControllerSourceTypePhotoLibrary value for the source type of your UIImagePickerController and the kUTTypeImage or kUTTypeMovie value, or both, for the media type, like so:

- (BOOL) isPhotoLibraryAvailable{ return [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]; } - (BOOL) canUserPickVideosFromPhotoLibrary{ return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary]; } - (BOOL) canUserPickPhotosFromPhotoLibrary{ return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary]; } - (void)viewDidLoad{ [super viewDidLoad]; if ([self isPhotoLibraryAvailable]){ UIImagePickerController *controller = [[UIImagePickerController alloc] init]; controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; NSMutableArray *mediaTypes = [[NSMutableArray alloc] init]; if ([self canUserPickPhotosFromPhotoLibrary]){ [mediaTypes addObject:(__bridge NSString *)kUTTypeImage]; } if ([self canUserPickVideosFromPhotoLibrary]){ [mediaTypes addObject:(__bridge NSString *)kUTTypeMovie]; } controller.mediaTypes = mediaTypes; controller.delegate = self; [self.navigationController presentModalViewController:controller animated:YES]; ...

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