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

时间:2014-01-22 10:18:39

标签: ios iphone objective-c xcode uiimagepickercontroller

我试图只使用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];

 }

3 个答案:

答案 0 :(得分:7)

使用UIImagePickerController,您只能获得一张照片。如果您需要选择更多,则需要自定义图像选择器,例如 ELCImagePickerController 。它运作良好!您可以下载here

答案 1 :(得分:7)

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

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

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

    您现在可以获取_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;
    

    那就是它。希望这会有所帮助。

答案 2 :(得分:0)

使用黑客攻击的主要原因包括将UIImagePickerController向上移动并在下方显示所选图像是因为资产库替代方案将涉及要求用户进行位置访问,因为有关图片元数据中可用照片的位置的信息

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

因此我认为像上面这样的黑客已经接近它们的实用性。 Here is a link to a library providing selection of multiple images using the Assets library还有其他人。

快乐编码!!!