2014-01-22 32 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

相關問題