如何从iOS中的UIImagePickerController中select多个图像

我试图简单地使用UIImagePickerController启用从photolibrary中select多个图像。我相对较新的XCode ,我不明白如何让用户从UIImagePickerControlerselect多个图像。 这是我目前的代码。请帮助任何机构如何从UIImagePickerControllerselect多个图像。

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

使用UIImagePickerController你只能得到一张图片。 如果你需要select更多,你需要一个自定义的图像select器,如ELCImagePickerController 。 它运作良好! 你可以在这里下载。

正如上面所说的,仅仅使用ImagePickerController是不可能的。 你需要自定义。 苹果公司最近推出了PHASSET Library,它使这一切变得简单。 开发者库中还有一个示例代码。 我在这里放下步骤。

  1. 设置您自己的collections视图
  2. 使用图库中的图片加载collections视图(使用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它。 希望这可以帮助。

使用像UIImagePickerController向下移动和显示选定图像在下面的黑客的主要原因是因为资产库的替代scheme将涉及用户被要求的位置访问,由于有关照片被拍摄的位置在图像元数据中可用的信息。

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

因此,我认为像上面这样的黑客已经接近他们的用处了。 这里是链接到一个图书馆提供select多个图像使用资产库还有其他。

快乐编码!