這樣的做法會根據照片的建立日期進行排序
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@”creationDate” ascending:YES],
];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:fetchOptions];
return [fetchResult firstObject];
但是這樣的排序結果跟在 蘋果照片的APP裡不同
PHFetchOptions *fetchOptions = [PHFetchOptions new];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:fetchOptions];
return [fetchResult firstObject];
將排序的Options移除, 就會得到與蘋果照片的APP相同的順序
如果我們要將照片反向排序, 只能依照這個方法. 這時候就沒有解法了?
[NSSortDescriptor sortDescriptorWithKey:@”creationDate” ascending:NO],
];
Get ufohead’s stories in your inbox
Join Medium for free to get updates from this writer.
嘗試將 fetchResult 做 NSEnumerationReverse 無效, 也嘗試針對 sortDescriptorWithKey 做其他選項
嘗試根據mediaType做filter, 並依據Image做filter
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@”mediaType” ascending:NO]];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@”mediaType = %d”,PHAssetMediaTypeImage];
似乎Xcode對此最佳化, sort變成無效
最後找到的方法 根據 mediaType 做排序, 並把mediaType設定為非Video
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@”mediaType” ascending:NO]];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@”mediaType != %d”,PHAssetMediaTypeVideo];
圖片順序會根據蘋果照片APP的反向來排序, 並且過濾掉影像檔案
打完收工
Reference :






