gpt4 book ai didi

iOS - ALAssetsGroupPropertyName 返回空专辑名称

转载 作者:行者123 更新时间:2023-11-29 01:51:49 25 4
gpt4 key购买 nike

我正在尝试检索所有相册名称和具有该相册名称的所有照片。我从 [group valueForProperty:ALAssetsGroupPropertyName] 得到的相册名称大部分是正确的,但有一个相册名称是 (null) 并且相册名为 Favorites 不显示。

专辑名称数组:

((null), Camera Roll, Last Import, Nissan Juke, Cam)

这是我的代码

-(void)getPhotosFromAssetsLibWithPhotoFilter:(NSString *)filterAlbumString
{
_assets = [@[] mutableCopy];
__block NSMutableArray *tmpAssets = [@[] mutableCopy];
__block NSMutableArray *albumGroup = [@[] mutableCopy];
ALAssetsLibrary *assetsLibrary = [PhotoLibViewController defaultAssetsLibrary];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result)
{
if (![filterAlbumString isEqualToString:@""])
{
if ([[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]] isEqualToString:filterAlbumString])
{
[tmpAssets addObject:result];
}
}
else
{
[tmpAssets addObject:result];
}
}
}];

[albumGroup addObject:[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]]];

dispatch_async(dispatch_get_main_queue(), ^{

if ([self respondsToSelector:@selector(retrievedPhotoLibrary:)])
{
NSArray *albumGroupReversed = [[albumGroup reverseObjectEnumerator] allObjects];
[self retrievedPhotoLibrary:albumGroupReversed];
}
});

self.assets = [[tmpAssets reverseObjectEnumerator] allObjects];

[self.collectionView reloadData];
} failureBlock:^(NSError *error) {
NSLog(@"Error loading images %@", error);
}];
}

我想知道为什么有一个(null)相册名称,为什么相册名称Favorites不显示。

谢谢。

最佳答案

当枚举完成后,enumerationBlock 被调用,group 设置为 nil,因此您应该在每次 group 是否为 nil 时检查并执行 reloadData 仅在枚举上完成。正确的逻辑应该是这样的:

[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil)
{
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result)
{
if (![filterAlbumString isEqualToString:@""])
{
if ([[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]] isEqualToString:filterAlbumString])
{
[tmpAssets addObject:result];
}
}
else
{
[tmpAssets addObject:result];
}
}
}];

[albumGroup addObject:[NSString stringWithFormat:@"%@", [group valueForProperty:ALAssetsGroupPropertyName]]];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{

if ([self respondsToSelector:@selector(retrievedPhotoLibrary:)])
{
NSArray *albumGroupReversed = [[albumGroup reverseObjectEnumerator] allObjects];
[self retrievedPhotoLibrary:albumGroupReversed];
}
});

self.assets = [[tmpAssets reverseObjectEnumerator] allObjects];

[self.collectionView reloadData];
}
} failureBlock:^(NSError *error) {
NSLog(@"Error loading images %@", error);
}];

对于 Favorites,它是 PhotoKit for iOS8 的新功能:

With iOS 8, Apple has given us PhotoKit, a modern framework that’s more performant than AssetsLibrary and provides features that allow applications to work seamlessly with a device’s photo library.

Favorite and Hidden Assets

To find out if an asset was marked as favorite or was hidden by the user, just inspect the favorite and hidden properties of the PHAsset instance.

See also: The Photos Framework

因此您必须通过PhotoKit 框架显示收藏夹,查看官方示例项目:ExampleappusingPhotosframework.zip .

关于iOS - ALAssetsGroupPropertyName 返回空专辑名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31315273/

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