19

我正在尝试使用 Photos 框架重现本机 iOS8 photopicker。我的排序有问题。

可以说我执行以下操作:

  1. 我在 Camera+ 应用程序中编辑照片并将其保存回图库。
  2. 我最喜欢另一张照片。

在本机照片选择器中:

  • 所有照片(甚至是我收藏的那张)都将按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
  • 我编辑的照片将在底部。这就是我在本机应用程序中想要和喜欢的。
  • 最喜欢的照片也将在底部:-(

因此,苹果似乎改变modificationDate了最喜欢的动作,也可能改变了其他类型的动作,这弄乱了照片的排序。

怎样才能准确地获得 Apple 在其原生应用程序中使用的排序?也许巧妙地使用NSSortDescriptor?

4

5 回答 5

9

迅速 3

let fetchOptions = PHFetchOptions()
    let sortOrder = [NSSortDescriptor(key: "creationDate", ascending: false)]
    fetchOptions.sortDescriptors = sortOrder
于 2017-04-27T03:50:24.213 回答
8

我刚刚发现要复制本机 photopicker 的确切行为,解决方案是删除我的自定义sortDescriptior并使用PHFetchResult默认行为。发现这一点后,现在看起来很明显。

于 2015-07-29T11:22:40.087 回答
5

正如@knutigro 提到的,解决方案是使用默认选项,PHFetchResult即通过传入参数:niloptions

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
}
于 2016-09-22T17:32:33.260 回答
2

您可以尝试使用两个排序描述符:

NSSortDescriptor sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; 

NSSortDescriptor sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:NO]];     

options.sortDescriptors = @[sortDesc1, sortDesc2];

没有测试,只是觉得可以用。

于 2015-07-29T09:50:06.363 回答
0

不要问我为什么以及如何,而是当我设置那种描述时。它像照片应用程序(最近)一样工作。

        [options setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"burstIdentifier" ascending:NO]]];

试试看。

于 2020-02-19T13:37:57.727 回答