2014-03-02 36 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