How to pick multiple images from UIImagePickerController in ios

24,793

Solution 1

With UIImagePickerController you are able to get only one picture. If you need to pick more you need a custom image picker, such as ELCImagePickerController. It works well! You can download it here.

Solution 2

As all said above, it is not possible using just ImagePickerController. You need to do it custom. Apple recently introduced PHASSET Library which makes this easy. There is a sample code also in the developer library. I am laying down the steps here.

  1. Setup your own collection view
  2. Load the collection view with pictures from gallery (using PHAsset, explained below)
  3. Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below)
  4. In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. Add it to a local array
  5. When done, read from the picture array and process

Snippet code for Loading Images from gallery.

         // Create a PHFetchResult object for each section in the table view.
    @property (strong, nonatomic) PHFetchResult *allPhotos;

    PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];

    if ( _isVideo == YES){
        _allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];

    }
    else {
        //_allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
        _allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];


    }

You now get all the images in your _allPhotos array which you will use like below in the cellForItemAtIndexPath

  PHAsset *asset = self.allPhotos[indexPath.item];

    //cell.representedAssetIdentifier = asset.localIdentifier;


    cell.selectedTick.hidden = YES;
    cell.isSelected = NO;

    // Request an image for the asset from the PHCachingImageManager.
    [self.imageManager requestImageForAsset:asset
                                 targetSize:CGSizeMake(100, 100)
                                contentMode:PHImageContentModeAspectFill
                                    options:nil
                              resultHandler:^(UIImage *result, NSDictionary *info) {
                                      cell.photo.image = result;
                              }];

    return cell;

Thatz it. Hope this helps.

Share:
24,793
user3222991
Author by

user3222991

Updated on November 22, 2020

Comments

  • user3222991
    user3222991 over 3 years

    I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController.I'm relatively new to XCode and I don't understand how to allow the user to pick multiple images from the UIImagePickerControler. This is my current code.Please help any body how to pick multiple images from UIImagePickerController.

     -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    
     {
          switch(buttonIndex)
          {
              case 0:
    
                  [self takeNewPhotoFromCamera];
                  break;
    
                  case 1:
                  [self choosePhotoFromExistingImages];
                  default:
    
                  break;
          }
    
     }
    
     - (void)takeNewPhotoFromCamera
    
     {
          if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
          {
              UIImagePickerController *controller = [[UIImagePickerController alloc] init];
              controller.sourceType = UIImagePickerControllerSourceTypeCamera;
              controller.allowsEditing = NO;
              controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
     UIImagePickerControllerSourceTypeCamera];
              controller.delegate = self;
              [self.navigationController presentViewController: controller animated: YES completion: nil];
          }
    
     }
    
     -(void)choosePhotoFromExistingImages
    
     {
          if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
          {
              UIImagePickerController *controller = [[UIImagePickerController alloc] init];
              controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
              controller.allowsEditing = NO;
              controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
     UIImagePickerControllerSourceTypePhotoLibrary];
              controller.delegate = self;
              [self.navigationController presentViewController: controller animated: YES completion: nil];
          }
    
     }
    
    
     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    
     {
          [self.navigationController dismissViewControllerAnimated: YES completion: nil];
          UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
          NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    
     }
    
     - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
    
     {
          [self.navigationController dismissViewControllerAnimated: YES completion: nil];
    
     }
    
    • Kumar KL
      Kumar KL over 10 years
      Can't do this by Picker. You need to use the custom
    • Kumar KL
      Kumar KL over 10 years
      If you just Look in google by "uiimagepickercontroller multiple image selection", You will get lot of possible solutions.
    • Saurav Mac
      Saurav Mac over 10 years
      Try this code stackoverflow.com/questions/9542487/… this may help you .
  • Tarun
    Tarun over 10 years
    Right, You need to use a custom image gallery. You may opt to write it yourself using image assest apis or may use any available 3rd party library which fulfills your requirement.
  • user3222991
    user3222991 over 10 years
    @Euroboy thanks for your response.IS there any alternative to with out using 3rd party framework to do the Multiselection images in UIImagepickercontroller. or told me any other alternative solution for that.
  • iOS Dev
    iOS Dev over 10 years
    @user3222991 I already said that with UIImagePickerController there is no way to do that. Just try ELCImagePickerController, it has similar behavior and does exactly what you need.
  • NSNoob
    NSNoob about 8 years
    You should add what is _isVideo flag info and also the method requestImageForAsset.
  • NSNoob
    NSNoob about 8 years
    In any case if anyone is wondering what is self.manager, it is a property which can be used like PHImageManager *manager = [PHImageManager defaultManager]
  • Hafeez
    Hafeez about 8 years
    Sorry, the _isVideo flag is a user set flag based on which the assets are fetched (videos or only photos). Also the "requestImageForAsset" is little tricky since by default it is async and you might get some weird results. I suggest a read [link]developer.apple.com/library/ios/documentation/Photos/R‌​eference/… . There is one more method "requestImageDataForAsset" which is simpler. Hope this helps.
  • Kishore Kumar
    Kishore Kumar almost 8 years
    @Hafeez please add more code it help full to some other :)