我如何仅使用 ALAssetsLibrary 获取视频

Posted

技术标签:

【中文标题】我如何仅使用 ALAssetsLibrary 获取视频【英文标题】:How do i get only the videos using ALAssetsLibrary 【发布时间】:2013-08-29 10:14:25 【问题描述】:

我正在尝试通过以下代码从照片库中获取视频。但我也获取图像列表。如何获取所有视频的列表?我做错了什么?

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

【问题讨论】:

【参考方案1】:

您需要在枚举期间将 ALAssetsFilter 添加到组中。这是一个基本示例:

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

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

                NSDictionary *meta = [[asset defaultRepresentation] metadata];

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

为了将来参考,available filters 是:

- allPhotos
- allVideos
- allAssets

【讨论】:

请注意,根据docs ALAssetsGroupAll 是“与除ALAssetsGroupLibrary 之外的所有组类型的ORing 相同”。如果您还需要 iTunes,请使用 ALAssetsGroupAll | ALAssetsGroupLibrary,其中“包括从 iTunes 同步的所有资产”。

以上是关于我如何仅使用 ALAssetsLibrary 获取视频的主要内容,如果未能解决你的问题,请参考以下文章

Objective-C/ALAssetslibrary - 从图像 exif 信息中获取 gps 纬度

如何释放从 ALAssetsLibrary 获取的 UIimage

在模拟器中重置 ALAssetsLibrary 的权限

如何根据目标c中的日期获取照片

ALAssetsLibrary 获取相机胶卷

iPhone ALAssetsLibrary 获取所有图像并进行编辑