gpt4 book ai didi

ios - 我如何使用 ALAssetsLibrary 只获取视频

转载 作者:可可西里 更新时间:2023-11-01 03:26:42 27 4
gpt4 key购买 nike

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

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, 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

关于ios - 我如何使用 ALAssetsLibrary 只获取视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18508178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com