gpt4 book ai didi

iphone - AlAssetLibrary 返回空结果

转载 作者:行者123 更新时间:2023-12-01 18:26:29 25 4
gpt4 key购买 nike

我正在尝试挑选一些相机胶卷元数据。当我枚举 Assets 时,我似乎无法检索任何信息并获得一个空数组。有没有我错过的步骤?

我的代码:

assets = [[NSMutableArray array] init]; 

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(asset != NULL) {
[assets addObject:asset];
dispatch_async(dispatch_get_main_queue(), ^{
});
}
};

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[group enumerateAssetsUsingBlock:assetEnumerator];
}
};

library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failed.");
}];

NSLog(@"%@", assets); //prints an empty array

最佳答案

Midhun MP 是正确的,您没有等待异步枚举完成。在这种情况下,您有异步 block 调用其他异步 block ,因此要知道所有枚举何时完成并不简单。

如果您想知道何时完成,并最终得到一个包含所有枚举 Assets 的数组,您可以使用 dispatch_groups。这是您可以做到这一点的一种方法(我包含了多个 ALAssetGroup 类型以表明这可以与多个专辑一起使用):

dispatch_group_t loadingGroup = dispatch_group_create();
NSMutableArray * assets = [[NSMutableArray array] init];
NSMutableArray * albums = [[NSMutableArray array] init];

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
if(index != NSNotFound) {
[assets addObject:asset];
dispatch_async(dispatch_get_main_queue(), ^{ });
} else {
dispatch_group_leave(loadingGroup);
}
};


void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
[albums addObject: group];
} else {
NSLog(@"Found %d albums", [albums count]);
// album loading is done
for (ALAssetsGroup * album in albums) {
dispatch_group_enter(loadingGroup);
[album enumerateAssetsUsingBlock: assetEnumerator];
}
dispatch_group_notify(loadingGroup, dispatch_get_main_queue(), ^{
NSLog(@"DONE: ALAsset array contains %d elements", [assets count]);
});
}
};

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

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos | ALAssetsGroupAlbum
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(@"Failed.");
}];

(在本例中,将各种 block 添加到 assetsalbums 是安全的,因为枚举都发生在主线程上。)

关于iphone - AlAssetLibrary 返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298912/

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