问题
When calling fetchAssetCollectionsWithType I have no trouble getting properly sorted results for most collection types. For example:
fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],];
PHFetchResult *topLevelUserCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];
I get all albums sorted correctly and if I reverse the sort order I get the opposite result.
When I do the same for PHAssetCollectionTypeSmartAlbum:
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"localizedTitle" ascending:YES],];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions];
The results don't come back in a particular order, though it is the same order every time. Reversing the order or changing the sort descriptor to something else like "endDate" has no effect on the results. I have verified that the items in fact have the properties like this:
for (PHAssetCollection *aSmartAlbum in smartAlbums){
NSLog(@"localizedTitle - %@, estimatedCount - %u", aSmartAlbum.localizedTitle, (int)(aSmartAlbum.estimatedAssetCount == NSNotFound ? 0 : aSmartAlbum.estimatedAssetCount));
}
This produces logs like this: localizedTitle - Videos, 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 - All Photos, estimatedCount - 0 localizedTitle - Hidden, estimatedCount - 0 localizedTitle - Recently Added, estimatedCount - 0
Has anyone gotten it to sort correctly?
I ended up basically pulling the results into an array and sorting them. I just happen to want the Camera Roll and favorites first.
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];
}
来源:https://stackoverflow.com/questions/32340880/can-fetchassetcollectionswithtypes-results-be-sorted-when-fetching-smart-albums