How to Save/Load Image/Videos from Camera Roll – XCode iOS

Learn how to implement UIImagePickerController for picking media from gallery or camera. Also learn how to save image or video to gallery.

For Image –

To Save Image to Camera Roll –
1
2
3
- (IBAction)SavePhotoOnClick:(id)sender{
    UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
}
To Load Image from Camera Roll –

1. Set the UIImagePickerDelegate in your ViewController.h file –

1
@interface ViewController : UIViewController

2. Add this method to your ViewController.m file. It may be a button press event.

1
2
3
4
5
6
7
- (IBAction)selectPhoto:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:NULL];
}

3. Implementing the Delegate Methods –

3a. This is called when the UIImagePickerController successfully picks up an image from the image roll.

1
2
3
4
5
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *selectedImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = selectedImage;
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

3b. This is called when user presses the Cancel button on the UIImagePickerView.

1
2
3
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

For Video –

To Save Video to Camera Roll –
1
UISaveVideoAtPathToSavedPhotosAlbum(videoPath,nil,nil,nil);

This can be used to save the newly captured video from camera to the gallery.

To Load Video From Camera Roll –

Its the same as we do for loading image. You just need to replace some code in the success delegate method
(check point 3a above). Replace the code inside the function with the following –

1
2
if ([mediaType isEqualToString:@"public.movie"]){
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
Tips –

1. We can put code for image and video together. We just need to wrap the code for picking image in an if condition as follows –

1
2
if ([mediaType isEqualToString:@"public.image"]){
}

2. When the image picker loads by default it shows just the images. If you want to see both the images and videos or just the videos, you can put the following in selectPhoto: method –

First add the MobileCoreServices.framework to your project. Also add #import <MobileCoreServices/UTCoreTypes.h> to your viewController.m file.

2a. For both –

1
picker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];

2b. For Image only –

1
[imagePicker setMediaTypes: [NSArray arrayWithObject:kUTTypeImage]];

Ideally you don’t need to put this, as by default image picker is set to image only.

2c. For Video only –

1
[imagePicker setMediaTypes: [NSArray arrayWithObject:kUTTypeMovie]];

3. You can pick the picture or video from camera using the same code just with slight modification –

1
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

If you are using camera, its better to put all the “picker” code inside the following condition –

1
2
3
if ([UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypeCamera]) {
}

As some old devices like iPad 1 does not have camera.

Have queries? do let me know in the comments.

Namit Gupta

iOS Consultant at @RapidCircle. Passionate about Mobile Apps. Loves to Design. You can reach him on his social profiles below.

Comments 8

    1. I have been searching for this in swift as well. I have tried several different solutions over the last few hours but to no avail.

      If there is a swift solution with latest version of xcode/swift please direct me towards it!

          1. Very cool, thank you. I need to select video only or both images and video. How do I translate the ObjC video code to Swift? Is it hard?

            Thank you !

Leave a Reply