6

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];

 }
Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
user3222991
  • 341
  • 3
  • 8
  • 16

3 Answers3

7

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.

iOS Dev
  • 4,143
  • 5
  • 30
  • 58
  • 1
    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. – Tarun Jan 22 '14 at 10:33
  • @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. – user3222991 Jan 22 '14 at 10:42
  • @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. – iOS Dev Jan 22 '14 at 10:48
7

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.

Hafeez
  • 403
  • 4
  • 7
  • You should add what is `_isVideo` flag info and also the method `requestImageForAsset`. – NSNoob Mar 24 '16 at 08:51
  • 2
    In any case if anyone is wondering what is `self.manager`, it is a property which can be used like `PHImageManager *manager = [PHImageManager defaultManager]` – NSNoob Mar 24 '16 at 08:53
  • 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]https://developer.apple.com/library/ios/documentation/Photos/Reference/PHImageManager_Class/ . There is one more method "requestImageDataForAsset" which is simpler. Hope this helps. – Hafeez Mar 26 '16 at 03:00
  • @Hafeez please add more code it help full to some other :) – Kishore Kumar May 04 '16 at 12:22
0

The main reason for using hacks like shifting the UIImagePickerController up and showing selected images underneath was because the asset library alternative would involve the user being asked for location access due to the information about where the photo was taken being available in the image metadata.

In iOS 6 the user is asked if they want to allow the app to access their photos (not location) and you get asked this question for both the asset library approach and the UIImagePickerController approach.

As such I think that hacks like the above are nearing the end of their usefulness. Here is a link to a library providing selection of multiple images using the Assets library there are others.

Happy Coding!!!

kagmanoj
  • 5,038
  • 5
  • 19
  • 21