我正在尝试构建一个简单的照片选择器,目前有两个选项:最近和收藏夹。我正在做的是尝试获取所有照片,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];
}
}
}
}