使用 PhotoKit 像本地照片应用程序一样对照片进行排序
Sorting photos exactly like native photoapp using PhotoKit
我正在尝试使用照片框架重现本机 iOS8 照片选择器。我在排序时遇到了问题。
假设我执行以下操作:
- 我在 Camera+ 应用程序中编辑照片并将其保存回图库。
- 我收藏了另一张照片。
在本机照片选择器中:
- 所有照片(包括我收藏的那张)将按
creationDate
排序
- 我编辑并保存的照片将在底部,因为它是我最后一次更改的照片。原件将根据其创建日期在其原始位置。
在我的应用程序中,我执行以下操作:
PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];
我在 creationDate
中排序并得到以下结果:
- 所有照片(包括我收藏的那张)将按
creationDate
排序
- 根据原始创建日期,我编辑的照片将紧随其后。
然后我更改我的查询,而不是按 creationDate
排序,而是按 modificationDate
排序。我得到以下结果:
- 所有照片的顺序大致相同
creationDate
- 我编辑的照片会在底部。这就是我在本机应用程序中想要和喜欢的。
- 最喜欢的照片也会在底部:-(
所以 Apple 似乎改变了 modificationDate
最喜欢的动作,可能也改变了其他类型的动作,这弄乱了照片的排序。
如何准确获得 Apple 在其本机应用程序中使用的排序?也许巧妙地使用 NSSortDescriptor
?
您可以尝试使用两个排序描述符:
NSSortDescriptor sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
NSSortDescriptor sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:NO]];
options.sortDescriptors = @[sortDesc1, sortDesc2];
还没有测试过,只是觉得它可能有用。
我刚刚发现要复制本机照片选择器的确切行为,解决方案是删除我的自定义 sortDescriptior
并仅使用具有默认行为的 PHFetchResult
。发现这一点后,现在看起来很明显。
正如@knutigro 提到的那样,解决方案是将 PHFetchResult
与默认选项一起使用,即通过为 options
参数传入 nil
:
var fetchResult: PHFetchResult!
override func viewDidLoad() {
super.viewDidLoad()
fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
}
但下一步是反转排序结果,以便最新的图像排在第一位。没有简单的方法来反转 PHFetchResult 中的结果,所以我使用了以下方法:
func assetAtIndex(index: Int) -> PHAsset {
// Return results in reverse order
return fetchResult[fetchResult.count - index - 1] as! PHAsset
}
swift 3
let fetchOptions = PHFetchOptions()
let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.sortDescriptors = sortOrder
不要问我为什么以及如何,但是当我设置那种描述时。它的工作原理类似于照片应用程序(最近)。
[options setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"burstIdentifier" ascending:NO]]];
试试吧。
我正在尝试使用照片框架重现本机 iOS8 照片选择器。我在排序时遇到了问题。
假设我执行以下操作:
- 我在 Camera+ 应用程序中编辑照片并将其保存回图库。
- 我收藏了另一张照片。
在本机照片选择器中:
- 所有照片(包括我收藏的那张)将按
creationDate
排序
- 我编辑并保存的照片将在底部,因为它是我最后一次更改的照片。原件将根据其创建日期在其原始位置。
在我的应用程序中,我执行以下操作:
PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];
我在 creationDate
中排序并得到以下结果:
- 所有照片(包括我收藏的那张)将按
creationDate
排序
- 根据原始创建日期,我编辑的照片将紧随其后。
然后我更改我的查询,而不是按 creationDate
排序,而是按 modificationDate
排序。我得到以下结果:
- 所有照片的顺序大致相同
creationDate
- 我编辑的照片会在底部。这就是我在本机应用程序中想要和喜欢的。
- 最喜欢的照片也会在底部:-(
所以 Apple 似乎改变了 modificationDate
最喜欢的动作,可能也改变了其他类型的动作,这弄乱了照片的排序。
如何准确获得 Apple 在其本机应用程序中使用的排序?也许巧妙地使用 NSSortDescriptor
?
您可以尝试使用两个排序描述符:
NSSortDescriptor sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
NSSortDescriptor sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:NO]];
options.sortDescriptors = @[sortDesc1, sortDesc2];
还没有测试过,只是觉得它可能有用。
我刚刚发现要复制本机照片选择器的确切行为,解决方案是删除我的自定义 sortDescriptior
并仅使用具有默认行为的 PHFetchResult
。发现这一点后,现在看起来很明显。
正如@knutigro 提到的那样,解决方案是将 PHFetchResult
与默认选项一起使用,即通过为 options
参数传入 nil
:
var fetchResult: PHFetchResult!
override func viewDidLoad() {
super.viewDidLoad()
fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: nil)
}
但下一步是反转排序结果,以便最新的图像排在第一位。没有简单的方法来反转 PHFetchResult 中的结果,所以我使用了以下方法:
func assetAtIndex(index: Int) -> PHAsset {
// Return results in reverse order
return fetchResult[fetchResult.count - index - 1] as! PHAsset
}
swift 3
let fetchOptions = PHFetchOptions()
let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.sortDescriptors = sortOrder
不要问我为什么以及如何,但是当我设置那种描述时。它的工作原理类似于照片应用程序(最近)。
[options setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"burstIdentifier" ascending:NO]]];
试试吧。