gpt4 book ai didi

ios - PHFetchResult 获取所有照片并按日期排序不一致

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

我正在尝试构建一个简单的照片选择器,目前有两个选项:最近和收藏夹。我正在做的是尝试在 creationDate 之前获取所有照片,但这是在我的数据源中以错误的顺序返回图像。数据源的开头是几年前的照片,还有不到几分钟的照片散落在各处。我认为问题是我需要先告诉主 fetchResult 排序顺序,但我认为这是不可能的:Unsupported sort descriptor in fetch options: (creationDate, ascending, compare:

如果能提供任何帮助,我将不胜感激。代码:

@property (nonatomic, strong) NSMutableOrderedSet *recentsDataSource;
@property (nonatomic, strong) NSMutableOrderedSet *favoritesDataSource;

- (void)setup
{
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

for (PHAssetCollection *sub in fetchResult)
{
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];

fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];

for (PHAsset *asset in assetsInCollection)
{
[self.recentsDataSource addObject:asset];

if (asset.isFavorite)
{
[self.favoritesDataSource addObject:asset];
}
}
}
}

最佳答案

这是我自己想出来的,这是我的解决方案:

- (void)setup
{
self.recentsDataSource = [[NSMutableOrderedSet alloc]init];
self.favoritesDataSource = [[NSMutableOrderedSet alloc]init];

PHFetchResult *assetCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];

PHFetchResult *favoriteCollection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumFavorites options:nil];

for (PHAssetCollection *sub in assetCollection)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];

for (PHAsset *asset in assetsInCollection)
{
[self.recentsDataSource addObject:asset];
}
}

if (self.recentsDataSource.count > 0)
{
NSArray *array = [self.recentsDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]];

self.recentsDataSource = [[NSMutableOrderedSet alloc]initWithArray:array];
}

for (PHAssetCollection *sub in favoriteCollection)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];

for (PHAsset *asset in assetsInCollection)
{
[self.favoritesDataSource addObject:asset];
}
}

if (self.favoritesDataSource.count > 0)
{
NSArray *array = [self.favoritesDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]];

self.favoritesDataSource = [[NSMutableOrderedSet alloc]initWithArray:array];
}
}

关于ios - PHFetchResult 获取所有照片并按日期排序不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602830/

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