使用ALAssetsLibrary从iphone相册中读取图像 [英] Read images from a iphone album using ALAssetsLibrary

查看:157
本文介绍了使用ALAssetsLibrary从iphone相册中读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个从相册中读取资源的方法我使用ALAsset从所有相册中获取所有图像,我的问题是如何自定义此方法以从myAlbum读取图像

I am trying to write a method that read assets from a album I am getting all images from all album using ALAsset, my question is how can I customize this method to read images from myAlbum

/**
 *  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"); }];
}

如何使用documentdataPath加载资产

How can I use documentdataPath to load assets

推荐答案

当您在块中调用 enumerateGroupsWithTypes 时,请检查资产组名称( valueForProperty:)。然后,您不能将所有资产组添加到 assetGroups 列表,而只能添加适当的资产组。

When you call enumerateGroupsWithTypes, in the block, check the asset group name (valueForProperty:). Then, instead of adding all of the asset groups to your assetGroups list you can only add the appropriate ones.

这篇关于使用ALAssetsLibrary从iphone相册中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆