ios – 如何使用ALAssetsLibrary仅获取视频

我试图从下面的代码片段中获取照片库中的视频.但我也得到图像列表.如何获得所有视频的列表?我究竟做错了什么?
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];
 xy =[[NSMutableArray alloc]init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

void (^assetEnumerator)( ALAsset *,NSUInteger,BOOL *) = ^(ALAsset *result,NSUInteger index,BOOL *stop) {
    if(result != nil) {
        if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
            [assetURLDictionaries addobject:[result valueForProperty:ALAssetPropertyURLs]];

            NSLog(@"result is:%@",result);
            NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
            NSURL *url= (NSURL*) [[result defaultRepresentation]url];

            [library assetForURL:url
                     resultBlock:^(ALAsset *asset) {
                         [xy addobject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                         NSLog(@" xy is:%@",xy);
                         image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
                         NSLog(@"image is:%@",image);
                     }
                    failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ];
        }
    }
};

NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
void (^ assetGroupEnumerator) ( ALAssetsGroup *,BOOL *)= ^(ALAssetsGroup *group,BOOL *stop) {
    NSLog(@"hello");
    if(group != nil) {
        [group enumerateAssetsUsingBlock:assetEnumerator];
        [assetGroups addobject:group];
        NSLog(@"Number of assets in group :%d",[group numberOfAssets]);
        NSLog(@"asset group is:%@",assetGroups);
    }
};

assetGroups = [[NSMutableArray alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll
                       usingBlock:assetGroupEnumerator
                     failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

解决方法

您需要在枚举期间向组中添加ALAssetsFilter.这是一个基本的例子:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group,BOOL *stop) {
    if (group) {
        [group setAssetsFilter:[ALAssetsFilter allVideos]];
        [group enumerateAssetsUsingBlock:^(ALAsset *asset,BOOL *stop){
            if (asset){

                NSDictionary *Meta = [[asset defaultRepresentation] Metadata];

            }
        }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error enumerating AssetLibrary groups %@\n",error);
}];

为了将来参考,available filters是:

- allPhotos
- allVideos
- allAssets

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...