2014-03-02 58 views
1

我試圖構建一個類似於iOS 7的照片應用的自定義圖像選擇器。我能夠從相機膠捲中挑選一張照片(ALAssetsGroupSavedPhotos),但我很難從另一張相冊中加載單張圖像 - 我爲測試目的而創建的一張相冊。ALAssetsLibrary從ALAssetsGroupAll加載單張照片

下面是我使用的加載從相機膠捲照片的代碼:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 

    [group setAssetsFilter:[ALAssetsFilter allPhotos]]; 
    numberOfPhotos = [group numberOfAssets]; 

    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) { 


          if (alAsset) { 

           ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
           UIImage *lastImage = [UIImage imageWithCGImage:[representation fullScreenImage]]; 


          } 

         }]; 

} 
        failureBlock: ^(NSError *error2) { 


        }]; 

我試圖取代ALAssetsGroupSavedPhotosALAssetsGroupAll但它返回以下錯誤:

Terminating app due to uncaught exception 'NSRangeException', reason: 'indexSet count or lastIndex must not exceed -numberOfAssets'

+0

你如何定義'index'? – RaffAl

+0

reecon索引是我試圖根據表格視圖單元格中的縮略圖位置選擇的照片的編號。謝謝。 – user1752054

回答

0

你正在枚舉庫中的所有文件夾並試圖從每個文件夾中獲取照片。這是錯誤的。首先你必須得到你的文件夾。

__block ALAssetsGroup* folder; 
NSString *yourFolderName = ...; 

[library enumerateGroupsWithTypes:ALAssetsGroupAlbum 
          usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
{ 
     if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:yourFolderName]) 
     { 
      folder = group; 
     } 
} 
          failureBlock:^(NSError* error) 
{ 
    // Error handling.  
}]; 

然後,從文件夾中獲得的圖像:

[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:index] 
         options:0 
        usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) 
{ 
    if (alAsset) 
    { 
     ALAssetRepresentation *representation = [alAsset defaultRepresentation]; 
     UIImage *lastImage = [UIImage imageWithCGImage:[representation fullScreenImage]]; 
    } 
}]; 

但要確保index是正確的。

+0

非常感謝澄清reecon。非常感激。一旦你到達文件夾=組,我該如何選擇,例如相冊中的照片編號3? – user1752054

+0

哇它的工作!非常感謝。你是一個救星reecon。 – user1752054

+0

沒問題的隊友:) – RaffAl