1

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?

4

2 回答 2

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

我将此代码用于特定专辑,但您可以修改所有专辑,希望对您有所帮助:)

于 2013-09-18T15:14:44.063 回答
0

下面的代码块计算所有视频和照片:

__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
 }];
于 2014-03-17T07:17:07.483 回答