Question

I enumerate my ALAssetGroup like this:

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {

    if (group.isEditable){
        NSLog(@"group is %@", group);
        [group setAssetsFilter:[ALAssetsFilter allPhotos]];
        [groups addObject:group];
    }

This filters the group to have only photos included in it's .numberOfAssets. However, I'd like to get both the photos count and the video count. How would I do that without enumerating the whole thing for the 2nd time?

Was it helpful?

Solution

ALAssetsLibrary *al = [[ALAssetsLibrary alloc]init];
    [al enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         if ([[group valueForProperty:ALAssetsGroupPropertyName]isEqualToString:@"MyAlbumName"]) {
             NSLog(@"in album");
             int nrAssets=[group numberOfAssets];

             __block int countVideo;
             __block int countPhoto;

            countPhoto=countVideo=0;
             [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
              {

                 if ([[asset valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypeVideo]) {
                          NSLog(@"eVideo ... count++");
                         countVideo++;
}
                else if(asset valueForProperty:ALAssetPropertyType]isEqualToString:AlAssetTypeVideo]){
                       NSLog(@"EPhoto ... ");
                       countPhoto++;
}

       }];
         }


     }
                    failureBlock:^(NSError *error) { NSLog(@"Boom!!!");}
     ];

i use this code for a specific album, but you can modify for all albums, hope it helped you :)

OTHER TIPS

The code block below counts all videos and photos:

__block int videoCount = 0;
__block int photoCount = 0;
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init];
[assetLibrary
 enumerateGroupsWithTypes:ALAssetsGroupAll
 usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
     if (group == nil) {
         // enumeration complete
         return;
     }
     int total = group.numberOfAssets;
     [group setAssetsFilter:[ALAssetsFilter allVideos]];
     int groupVideoCount = group.numberOfAssets;
     videoCount += groupVideoCount;
     photoCount += total - groupVideoCount;
 }
 failureBlock:^(NSError *error) {
     // Handle error
 }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top