如何在 ios 中从 UIImagePickerController 中选取多个图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21280063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:12:44  来源:igfitidea点击:

How to pick multiple images from UIImagePickerController in ios

iosiphoneobjective-cxcodeuiimagepickercontroller

提问by user3222991

I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController.I'm relatively new to XCodeand 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.

我试图简单地使用UIImagePickerController.I从照片库中选择多个图像。XCode我相对较新,我不明白如何允许用户从UIImagePickerControler. 这是我当前的代码。请帮助任何机构如何从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];

 }

回答by iOS Dev

With UIImagePickerControlleryou 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.

随着UIImagePickerController你就能得到的只有一张图片。如果您需要选择更多,则需要自定义图像选择器,例如ELCImagePickerController。它运作良好!你可以在这里下载。

回答by Hafeez

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.

如上所述,仅使用 ImagePickerController 是不可能的。你需要定制。Apple 最近推出了 PHASSET 库,使这变得容易。开发人员库中也有示例代码。我在这里放下台阶。

  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
  1. 设置您自己的集合视图
  2. 使用图库中的图片加载集合视图(使用 PHAsset,如下所述)
  3. 显示 cellForItemAtIndexPath 中的每张图片(使用 PHAsset,解释如下)
  4. 在您的 didSelectItemAtIndexPath 中,跟踪选择了哪些图片并添加刻度标记图像。将其添加到本地数组
  5. 完成后,从图片数组中读取并处理

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

您现在将获得 _allPhotos 数组中的所有图像,您将在 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.

那就这样吧。希望这可以帮助。

回答by kagmanoj

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.

使用像上移 UIImagePickerController 并在下方显示选定图像这样的技巧的主要原因是,资产库替代方案将涉及要求用户访问位置,因为图像元数据中提供了有关照片拍摄地点的信息。

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.

在 iOS 6 中,系统会询问用户是否允许应用访问他们的照片(而不是位置),并且您会针对资产库方法和 UIImagePickerController 方法询问这个问题。

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 librarythere are others.

因此,我认为像上面这样的黑客已经接近其有用性的尽头。这是一个库的链接,使用资产库提供多个图像的选择,还有其他

Happy Coding!!!

编码快乐!!!