Read images from a iphone album using ALAssetsLibrary

对着背影说爱祢 提交于 2020-01-13 10:58:39

问题


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

How can I use documentdataPath to load assets


回答1:


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.



来源:https://stackoverflow.com/questions/19297396/read-images-from-a-iphone-album-using-alassetslibrary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!