Objective-C | Access Photo Library using UIImagePickerController

Open photo library in Objective-C

In this tutorial, I will show you how to open a native photo library controller and also intercept the selected image. Let’s start by adding the protocols we would need UINavigationControllerDelegate and UIImagePickerControllerDelegate…

Open UIImagePickerController:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
Then you can intercept the selected image by using this delegate method:

// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//    You can retrieve the actual UIImage
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//    Or you can get the image url from AssetsLibrary
    NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
}

If you need this feature for Swift, you can visit that tutorial here!

Recommended for you: iOS 11 – 100 NEW Hidden Features & Changes

Follow me on Medium, for more interesting Swift programming tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *