ios - 如何使用 fetchAssests 按添加日期的顺序获取 PHAssets

标签 ios objective-c iphone photokit

我正在尝试按照用户添加的相同顺序从相机胶卷、iCloud 和照片流中获取 PHAsset。在 iOS 11 中,我使用

PHFetchOptions *options = [[PHFetchOptions alloc] init];
PHFetchResult *fetchResults = [PHAsset fetchAssetsWithOptions:options];

但这在 iOS 12 中已停止工作。使用此 iCloud 图像和相机胶卷图像是分开的。只有 2 个排序描述符提供了 creationDate 和 modifiedDate。

我尝试的第二个解决方案是使用智能相册从“用户库”中获取照片。根据用户是否打开 iCloud 库,“用户库”的工作方式有所不同。如果 iCloud 库打开,则用户库包含相机胶卷和 iCloud 库中的所有图像。如果关闭 iCloud 库,则用户库仅包含相机胶卷中的图像。这会以正确的顺序返回相机胶卷和 iCloud 库中的图像。问题是它无法从 Photostream 获取图像。

在另一个解决方案中,如果 iCloud 库打开,照片从用户库中获取,如果 iCloud 关闭,则使用旧方法获取照片流和相机胶卷图像的正确顺序。

此解决方案也无效。

请检查以下场景:

  • iCloud 图库已关闭,Photostream 已打开。我们将使用带有默认选项的 fetchAssetsWithOption。顺序将是正确的。
  • 用户打开 iCloud。照片流自动关闭。我们将从“用户库”中获取照片。为我们提供 iCloud 中的所有图像以及使用默认选项排序的相机胶卷。顺序将是正确的。
  • 现在用户关闭了 iCloud 库。 iCloud 中的所有图像都应该从设备中删除。但是,一些来自 iCloud 的图像留在相机胶卷中。它们不会被删除(可能是由于 iCloud 中的错误)。结果,这些未删除的图像总是出现在最上面。如果用户从 iCloud 下载图像并且在 iCloud 关闭时没有删除图像,也会发生这种情况。

那么有什么办法可以做到吗

最佳答案

iOS 13 的答案正在更新,其中使用“smartAlbumRecentlyAdded”比“smartAlbumUserLibrary”效果更好,因为行为发生了变化:已向 Apple 发送有关 iOS 13 行为变化的反馈。希望有更好的解决方案.

因为,尽管 smartAlbumRecentlyAdded 会在添加日期之前返回项目,但您不会获得所有项目,只会获得最近的项目。

    var fetchResult:PHFetchResult<PHAssetCollection>

    if #available(iOS 13.0, *) {
        fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumRecentlyAdded,options: nil)
    }
    else {
        fetchResult = PHAssetCollection.fetchAssetCollections(with:.smartAlbum,subtype:.smartAlbumUserLibrary,options: nil)
    }

    if let assetCollection = fetchResult.firstObject {
        self.allPhotos = PHAsset.fetchAssets(in: assetCollection, options: nil)
    }

关于ios - 如何使用 fetchAssests 按添加日期的顺序获取 PHAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314469/

相关文章:

iOS 8 - 无法安装企业应用程序

iphone - 配对dispatch_resume和dispatch_suspend以强制终止应用程序

iphone - 摆脱 UITabBar 上的白色 strip

ios - Swift 相等需要 ObjectIdentifier 吗?

objective-c - objective-c : Cell text disappears after scrolling out of screen (and back)

ios - 在 subview 中添加 UIViewController

objective-c - nstableview 重新加载不起作用

ios - 苹果开发者证书

ios - 如何在 ios 应用程序中绘制图钉问题?

iphone - 如何使用 Objective-C 为两个 View 设置动画