简体   繁体   English

如何从ios中的UIImagePickerController中选择多个图像

[英]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 . 我试图简单地使用UIImagePickerController从photolibrary中选择多个图像。我对XCode相对较新,我不明白如何允许用户从UIImagePickerControler选择多个图像。 This is my current code.Please help any body how to pick multiple images from UIImagePickerController . 这是我目前的代码。请帮助任何人如何从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];

 }

With UIImagePickerController you are able to get only one picture. 使用UIImagePickerController您只能获得一张图片。 If you need to pick more you need a custom image picker, such as ELCImagePickerController . 如果您需要选择更多,则需要自定义图像选择器,例如ELCImagePickerController It works well! 它运作良好! You can download it here . 你可以在这里下载。

As all said above, it is not possible using just ImagePickerController. 如上所述,仅使用ImagePickerController是不可能的。 You need to do it custom. 你需要自定义。 Apple recently introduced PHASSET Library which makes this easy. Apple最近推出了PHASSET Library,这让这很容易。 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) 使用库中的图片加载集合视图(使用PHAsset,如下所述)
  3. Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below) 显示cellForItemAtIndexPath中的每个图片(使用PHAsset,如下所述)
  4. In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. 在didSelectItemAtIndexPath中,跟踪选择的图片并添加刻度线图像。 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 你现在得到_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. 希望这可以帮助。

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 library there are others. 以下是使用Assets库提供多个图像选择的库的链接

Happy Coding!!! 快乐的编码!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM