MacOS PhotoKit - 列出所有用户创建的智能相册?
MacOS PhotoKit - List all user-created Smart Albums?
我是第一次接触 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。如您在此屏幕截图中所见,我定义了 18 个智能相册和 1 个常规相册。
但是,当我获取智能相册 PHAssetCollection
s 时,我没有看到我定义的智能相册。这是我用来获取照片总数、智能相册和普通相册的代码:
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
如您所见,返回了我的普通相册 我的测试相册,但返回了我创建的 none 智能相册,例如 2008、2009、等我做错了什么吗?除了包含 NSPhotoLibraryUsageDescription
之外,我的 Info.plist
还需要额外的值吗?这是我第一次使用 PhotoKit 框架,所以我确定我可能误解了一些东西。但是,我认为这是一个非常简单的用例。我 运行 在 macOS Catalina 上使用 Xcode 11.5 和 Swift.
TL;DR - 这不可能。
- 内置智能相册 = 自拍、全景、截图...
- 智能相册 = 在 Mac
上创建的自定义智能相册
Apple 很久以前就推出了照片应用程序(iPhoto 的继任者)。如果您是需要智能相册、关键字等的高级用户,它过去是,现在仍然是相当有限的……打开 phone 上的照片应用程序 - 没有智能相册。访问 iCloud Photos - 没有智能相册。它们未同步1.
当您查看 smartAlbum
文档时,它说:
The Photos app displays built-in smart albums to group certain kinds of related assets.
我不知道他们为什么在 Photos.framework 文档中谈论照片应用程序,它不是很清楚,但它给了你一个线索,即 smartAlbum
是关于内置智能相册。
还有 smartAlbumGeneric
子类型文档:
This subtype applies to smart albums synced to the iOS device from the macOS Photos app.
但是没有智能相册同步到iOS设备。如果您尝试使用这个列表,它是一个空列表。
由于 iCloud 照片(恕我直言),它在某种程度上受到限制。在另一个框架中有一个类比 - CoreData + iCloud 支持对您可以做的事情施加了一些限制,并且它不如 CoreData(没有 iCloud)强大。但这纯粹是猜测。如果您关闭 iCloud 照片(照片 - 首选项... - iCloud - iCloud 照片),也许你所有的智能相册都会出现在列表中,但我没有那么勇敢尝试,因为我不想再次下载整个图库或者丢东西。
您要访问您的智能相册吗?一些解决方法:
- 打开相册,select所有照片并创建一个普通相册 -> 手动同步它们,
- Create Standard Albums from Smart Albums to Sync with iCloud to iOS Devices -> 半手动同步,
- 您可以搜索更多、很多文章、问题、...
1 实际上它们是同步的,但您只能在 Mac(s) 上看到它们。
我是第一次接触 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。如您在此屏幕截图中所见,我定义了 18 个智能相册和 1 个常规相册。
但是,当我获取智能相册 PHAssetCollection
s 时,我没有看到我定义的智能相册。这是我用来获取照片总数、智能相册和普通相册的代码:
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
如您所见,返回了我的普通相册 我的测试相册,但返回了我创建的 none 智能相册,例如 2008、2009、等我做错了什么吗?除了包含 NSPhotoLibraryUsageDescription
之外,我的 Info.plist
还需要额外的值吗?这是我第一次使用 PhotoKit 框架,所以我确定我可能误解了一些东西。但是,我认为这是一个非常简单的用例。我 运行 在 macOS Catalina 上使用 Xcode 11.5 和 Swift.
TL;DR - 这不可能。
- 内置智能相册 = 自拍、全景、截图...
- 智能相册 = 在 Mac 上创建的自定义智能相册
Apple 很久以前就推出了照片应用程序(iPhoto 的继任者)。如果您是需要智能相册、关键字等的高级用户,它过去是,现在仍然是相当有限的……打开 phone 上的照片应用程序 - 没有智能相册。访问 iCloud Photos - 没有智能相册。它们未同步1.
当您查看 smartAlbum
文档时,它说:
The Photos app displays built-in smart albums to group certain kinds of related assets.
我不知道他们为什么在 Photos.framework 文档中谈论照片应用程序,它不是很清楚,但它给了你一个线索,即 smartAlbum
是关于内置智能相册。
还有 smartAlbumGeneric
子类型文档:
This subtype applies to smart albums synced to the iOS device from the macOS Photos app.
但是没有智能相册同步到iOS设备。如果您尝试使用这个列表,它是一个空列表。
由于 iCloud 照片(恕我直言),它在某种程度上受到限制。在另一个框架中有一个类比 - CoreData + iCloud 支持对您可以做的事情施加了一些限制,并且它不如 CoreData(没有 iCloud)强大。但这纯粹是猜测。如果您关闭 iCloud 照片(照片 - 首选项... - iCloud - iCloud 照片),也许你所有的智能相册都会出现在列表中,但我没有那么勇敢尝试,因为我不想再次下载整个图库或者丢东西。
您要访问您的智能相册吗?一些解决方法:
- 打开相册,select所有照片并创建一个普通相册 -> 手动同步它们,
- Create Standard Albums from Smart Albums to Sync with iCloud to iOS Devices -> 半手动同步,
- 您可以搜索更多、很多文章、问题、...
1 实际上它们是同步的,但您只能在 Mac(s) 上看到它们。