2014-01-22 33 views
3

我想简单地启用从UIImagePickerController中选择photolibrary中的多个图像。我是XCode的新手,我不明白如何让用户从多个图像中选择多个图像UIImagePickerControler。这是我目前的代码。请帮助任何机构如何从UIImagePickerController中挑选多个图像。如何在ios中从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]; 

} 
+0

Picker无法做到这一点。你需要使用自定义 –

+0

如果你只是看看谷歌的“uiimagepickercontroller多图像选择”,你会得到很多可能的解决方案。 –

+0

试试这个代码http://stackoverflow.com/questions/9542487/select-multiple-images-from-photo-library这可能会帮助你。 –

回答

7

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

+1

对,您需要使用自定义图片库。您可以选择使用图像信息API自己编写它,也可以使用任何可用的第三方库来满足您的要求。 – Tarun

+0

@Euroboy感谢您的回复。除了使用第三方框架来完成UIImagepickercontroller中的Multiselection图像,还有没有其他选择。或者告诉我任何其他的替代解决方案。 – user3222991

+0

@ user3222991我已经说过用'UIImagePickerController'就没有办法做到这一点。只需尝试'ELCImagePickerController',它具有类似的行为,并且完全符合您的需求。 –

0

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

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

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

快乐编码!

5

正如前面所说的,仅仅使用ImagePickerController是不可能的。你需要自定义。苹果公司最近推出了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]; 


    } 

现在你得到所有你_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它使用像下面的图片。希望这可以帮助。

+0

您应该添加'_isVideo'标志信息以及'requestImageForAsset'方法。 – NSNoob

+1

在任何情况下,如果有人想知道什么是'self.manager',它是一个可以像'PHImageManager * manager = [PHImageManager defaultManager]'使用的属性' – NSNoob

+0

对不起,_isVideo标志是一个用户设置标志,资产被抓取(视频或仅照片)。此外,“requestImageForAsset”有点棘手,因为默认情况下它是异步的,你可能会得到一些奇怪的结果。我建议阅读[链接] https://developer.apple.com/library/ios/documentation/Photos/Reference/PHImageManager_Class/。还有一个更简单的方法“requestImageDataForAsset”。希望这可以帮助。 – Hafeez

相关问题