尝试使用ALAssetsLibrary检索一组照片

时间:2013-11-25 07:38:42

标签: ios iphone alassetslibrary photo-tagging

我目前正在开发一个实现面部检测和标记的iPhone应用程序。我正在使用SQLite数据库来存储标记和相应图像的URL。现在,在检索时,我将实现一些逻辑,根据标签过滤掉所需的图像,并获取与此标记匹配的图像URL集(来自Db)。 (资产库URL的形式 - assets-library://asset/asset.JPG?id = 79465E8C-53B9-40D6-B11C-07A1856E9093& ext = JPG)

我的问题是,如果我有一个NSURL数组,如何使用ALAssetsLibrary加载自定义图像选择器,只有数组中存在的URL而不是默认照片库中的所有图像?

我已经阅读了如何基于此答案从URL加载图像: https://stackoverflow.com/a/18888938

这个问题: display image from URL retrieved from ALAsset in iPhone

如何在我的URL数组上运行单个循环,以使用ALAssets将这些图像加载到自定义UICollectionView中以复制ImagePickerController?

2 个答案:

答案 0 :(得分:0)

从Apple查看此sample code以实现自定义图像选择器。

答案 1 :(得分:0)

/**
 *  This method is used to get all images from myAlbum 
 *  folder in device if getAlbumImages is set to 1
 *  else loads all images from devices library
 */
-(void)readImages:(int)getAlbumImages
{
    imageArray = [[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    if(getFolderImages == 1) {
        // Load images from Shareaflash folder
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *documentdataPath = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
        NSLog(@"documentdataPath %@",documentdataPath);

    } else {
        // Load all images
    }

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url = (NSURL*) [[result defaultRepresentation]url];

                [library assetForURL:url resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:asset];

                             if ([mutableArray count]==count)
                             {
                                 imageArray =[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];
            }
        }
    };
    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };
    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                         usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) { NSLog(@"There is an error"); }];
}