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

use*_*991 6 iphone xcode objective-c uiimagepickercontroller ios

我试图简单地启用从photolibrary中选择多个图像使用.我UIImagePickerController相对较新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];

 }
Run Code Online (Sandbox Code Playgroud)

iOS*_*Dev 7

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


Haf*_*eez 7

如上所述,仅使用ImagePickerController是不可能的.你需要自定义.Apple最近推出了PHASSET Library,这让这很容易.开发人员库中也有一个示例代码.我正在铺设这里的步骤.

  1. 设置您自己的集合视图
  2. 使用库中的图片加载集合视图(使用PHAsset,如下所述)
  3. 显示cellForItemAtIndexPath中的每个图片(使用PHAsset,如下所述)
  4. 在didSelectItemAtIndexPath中,跟踪选择的图片并添加刻度线图像.将其添加到本地阵列
  5. 完成后,从图片数组中读取并处理

用于从库中加载图像的代码段.

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


    }
Run Code Online (Sandbox Code Playgroud)

你现在得到_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;
Run Code Online (Sandbox Code Playgroud)

那就是它.希望这可以帮助.

  • 在任何情况下,如果有人想知道什么是`self.manager`,它是一个可以像`PHImageManager*manager = [PHImageManager defaultManager]一样使用的属性. (2认同)