调用fetchAssetCollectionsWithType时,我可以轻松获取大多数集合类型的正确排序结果。例如:
fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],];
PHFetchResult *topLevelUserCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];
我将所有专辑正确排序,如果我颠倒排序顺序,我会得到相反的结果。
当我对PHAssetCollectionTypeSmartAlbum执行相同操作时:
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
结果不按特定顺序返回,但每次都是相同的顺序。撤消订单或将排序描述符更改为“endDate”之类的其他内容对结果没有影响。我已经确认这些项目实际上具有以下属性:
for (PHAssetCollection *aSmartAlbum in smartAlbums){
NSLog(@"localizedTitle - %@, estimatedCount - %u", aSmartAlbum.localizedTitle, (int)(aSmartAlbum.estimatedAssetCount == NSNotFound ? 0 : aSmartAlbum.estimatedAssetCount));
}
这会生成如下日志: localizedTitle - 视频,estimatedCount - 0 localizedTitle - Panoramas,estimatedCount - 0 localizedTitle - Time-lapse,estimatedCount - 0 localizedTitle - Slo-mo,estimatedCount - 0 localizedTitle - Bursts,estimatedCount - 0 localizedTitle - Favorites,estimatedCount - 0 localizedTitle - 所有照片,estimatedCount - 0 localizedTitle - 隐藏,estimatedCount - 0 localizedTitle - 最近添加,estimatedCount - 0
有没有人让它正确排序?
我最终基本上将结果拉入数组并对其进行排序。我碰巧首先想要相机胶卷和收藏夹。
PHFetchOptions *fetchOptions = [PHFetchOptions new];
PHFetchResult* unorderedSmart = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
PHAssetCollection* allPhotos;
PHAssetCollection* favorites;
NSMutableArray* orderedSmart = [NSMutableArray array];
for (PHAssetCollection *aSmartAlbum in unorderedSmart){
if (aSmartAlbum.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) {
allPhotos = aSmartAlbum;
}else if (aSmartAlbum.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumFavorites){
favorites = aSmartAlbum;
}else{
[orderedSmart addObject:aSmartAlbum];
}
}
[orderedSmart sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
PHAssetCollection* ac1 = obj1;
PHAssetCollection* ac2 = obj2;
return [ac1.localizedTitle localizedCaseInsensitiveCompare:ac2.localizedTitle];
}];
if (favorites) {
[orderedSmart insertObject:favorites atIndex:0];
}
if (allPhotos) {
[orderedSmart insertObject:allPhotos atIndex:0];
}