ioscocoa-touchuikitalassetslibraryalasset

Get counts of photos and videos in ALAssetGroup


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?


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 :)