gpt4 book ai didi

swift - MacOS PhotoKit - 列出所有用户创建的智能相册?

转载 作者:行者123 更新时间:2023-12-02 02:51:03 29 4
gpt4 key购买 nike

我第一次开始尝试 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。正如您在此屏幕截图中看到的,我定义了 18 个智能相册和 1 个常规相册。

Screenshot of the Albums

但是,当我获取智能相册 PHAssetCollection 时,我看不到我定义的智能相册。以下是我用来获取照片总数、智能相册和普通相册的代码:

let allPhotosOptions = PHFetchOptions()
allPhotosOptions.includeAssetSourceTypes = [.typeUserLibrary]

allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)
let photoCount = allPhotos.count
print("photoCount=\(photoCount)")

print("***********************************")
print("Smart Albums")
print("***********************************")

let smartAlbumsOptions = PHFetchOptions()
smartAlbumsOptions.includeAssetSourceTypes = [.typeUserLibrary]
let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: smartAlbumsOptions)
let smartAlbumCount = smartAlbums.count
print("smartAlbumCount=\(smartAlbumCount)")
smartAlbums.enumerateObjects { (collection, index, stop) in
print("Collection \(index + 1):")
print("\tlocalizedTitle=\(String(describing: collection.localizedTitle))")
print("\tassetCollectionType=\(collection.assetCollectionType)")
print("\testimatedAssetCount=\(collection.estimatedAssetCount)")
print("\tstartDate=\(String(describing: collection.startDate)), endDate=\(String(describing: collection.endDate))")
}

print("***********************************")
print("Albums")
print("***********************************")

let albums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
let albumCount = albums.count
print("albumCount=\(albumCount)")
albums.enumerateObjects { (collection, index, stop) in
print("Collection \(index + 1):")
print("\tlocalizedTitle=\(String(describing: collection.localizedTitle))")
print("\tassetCollectionType=\(collection.assetCollectionType)")
print("\testimatedAssetCount=\(collection.estimatedAssetCount)")
print("\tstartDate=\(String(describing: collection.startDate)), endDate=\(String(describing: collection.endDate))")
}

但是,当我运行此代码时,我看到以下输出,显示我的用户创建的智能相册没有被返回:

2020-05-23 16:41:31.059604-0400 PhotoKitTest[40065:709882] Metal API Validation Enabled
2020-05-23 16:41:33.027378-0400 PhotoKitTest[40065:709882] [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""
photoCount=25369
***********************************
Smart Albums
***********************************
smartAlbumCount=15
Collection 1:
localizedTitle=Optional("Bursts")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2017-03-13 14:50:57 +0000), endDate=Optional(2018-08-29 15:15:03 +0000)
Collection 2:
localizedTitle=Optional("Slo-mo")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 3:
localizedTitle=Optional("Screenshots")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2015-10-22 13:51:51 +0000), endDate=Optional(2020-05-21 14:39:40 +0000)
Collection 4:
localizedTitle=Optional("Animated")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 5:
localizedTitle=Optional("Live Photos")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 6:
localizedTitle=Optional("Unable to Upload")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 7:
localizedTitle=Optional("Panoramas")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2015-06-29 22:14:27 +0000), endDate=Optional(2015-07-26 19:28:39 +0000)
Collection 8:
localizedTitle=Optional("Videos")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2004-11-11 01:03:36 +0000), endDate=Optional(2020-05-23 14:52:01 +0000)
Collection 9:
localizedTitle=Optional("Hidden")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 10:
localizedTitle=Optional("Selfies")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2011-08-06 13:17:49 +0000), endDate=Optional(2020-05-21 14:33:38 +0000)
Collection 11:
localizedTitle=Optional("Favorites")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 12:
localizedTitle=Optional("Time-lapse")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 13:
localizedTitle=Optional("Portrait")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 14:
localizedTitle=Optional("Long Exposure")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=nil, endDate=nil
Collection 15:
localizedTitle=Optional("Recents")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=9223372036854775807
startDate=Optional(2001-09-22 16:05:17 +0000), endDate=Optional(2020-05-23 14:52:01 +0000)
***********************************
Albums
***********************************
albumCount=1
Collection 1:
localizedTitle=Optional("My Test Album")
assetCollectionType=PHAssetCollectionType
estimatedAssetCount=0
startDate=nil, endDate=nil

如您所见,我的正常相册我的测试相册已返回,但没有返回我创建的智能相册,例如2008年、2009年等的智能相册。我是否做错了什么?除了包含 NSPhotoLibraryUsageDescription 之外,我的 Info.plist 中还需要其他值吗?这是我第一次使用 PhotoKit 框架,所以我确信我可能误解了一些东西。但是,我认为这是一个非常简单的用例。我使用 Xcode 11.5 和 Swift 在 macOS Catalina 上运行此程序。

最佳答案

TL;DR - 这是不可能的。


  • 内置智能相册=自拍、全景图、屏幕截图……
  • 智能相册 = 在 Mac 上创建的自定义智能相册

Apple 很久以前就推出了照片应用程序(iPhoto 的后继者)。如果您是需要智能相册、关键字等的高级用户,那么它过去是、现在仍然是相当有限的。打开手机上的照片应用程序 - 没有智能相册。访问iCloud Photos - 没有智能相册。它们未同步1

当您检查smartAlbum时文档,它说:

The Photos app displays built-in smart albums to group certain kinds of related assets.

我不知道为什么他们在 Photos.framework 文档中谈论照片应用程序,它不是很清楚,但它给了你一个线索,即 smartAlbum 是关于 < strong>内置智能相册。

还有smartAlbumGeneric子类型文档:

This subtype applies to smart albums synced to the iOS device from the macOS Photos app.

但没有智能相册同步到 iOS 设备。如果您尝试使用这个列表,它是一个空列表。

由于 iCloud Photos(恕我直言),它在某种程度上受到限制。另一个框架中有一个类比 - CoreData + iCloud 支持对您可以做的事情施加了一些限制,并且它不如 CoreData(没有 iCloud)强大。但这纯粹是猜测。如果您关闭 iCloud 照片(照片 - 偏好设置... - iCloud - iCloud 照片),也许您所有的智能相册都会出现在列表中,但我没有那么勇敢地尝试它,因为我不想再次下载整个图库或者失去一些东西。

您想访问您的智能相册吗?一些解决方法:


1 实际上它们是同步的,但您只能在 Mac 上看到它们。

关于swift - MacOS PhotoKit - 列出所有用户创建的智能相册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61978366/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com