Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick multiple images from UIImagePickerController in ios

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

 }
like image 864
user3222991 Avatar asked Jan 22 '14 10:01

user3222991


People also ask

How do you click multiple pictures on flutter?

To select multiple images we will use multi_image_picker flutter package. in this Package we can select multiple images from gallery.

What is image picker iOS?

A view controller that manages the system interfaces for taking pictures, recording movies, and choosing items from the user's media library. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+


2 Answers

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.

like image 121
iOS Dev Avatar answered Oct 31 '22 16:10

iOS Dev


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.

like image 30
Hafeez Avatar answered Oct 31 '22 16:10

Hafeez