繁体   English   中英

使用ALAssetsLibrary从iphone相册中读取图像

[英]Read images from a iphone album using ALAssetsLibrary

我正在尝试编写一个从相册中读取资源的方法我使用ALAsset从所有相册中获取所有图像,我的问题是如何自定义此方法以从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加载资源

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM