Ios 可以获取AssetCollectionSwithType';s在获取智能相册时是否对结果进行排序?
调用fetchAssetCollectionsWithType时,对于大多数集合类型,获得正确排序的结果并没有问题。例如:Ios 可以获取AssetCollectionSwithType';s在获取智能相册时是否对结果进行排序?,ios,photokit,Ios,Photokit,调用fetchAssetCollectionsWithType时,对于大多数集合类型,获得正确排序的结果并没有问题。例如: fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],]; PHFetchResult *topLevelUserCollec
fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],];
PHFetchResult *topLevelUserCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];
我将所有相册正确排序,如果我颠倒排序顺序,我将得到相反的结果
当我对PhaseSetCollectionTypeSmartAlbum执行相同操作时:
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));
}
这将生成如下日志:
本地化标题-视频,估计数量-0
本地化标题-全景,估计数量-0
localizedTitle-时间推移,估计数-0
本地化标题-Slo mo,估计数量-0
localizedTitle-突发,估计数-0
localizedTitle-收藏夹,估计数-0
本地化标题-所有照片,估计数量-0
localizedTitle-隐藏,估计数-0
localizedTitle-最近添加,估计数-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];
}
有同样的问题,我一直在寻找有效的解决方案几个小时没有运气!你看,我在上面回答了自己,这真的是我找到的最好的解决办法。