我第一次开始玩 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。正如您在此屏幕截图中看到的,我定义了 18 个智能相册和 1 个常规相册。
但是,当我获取 Smart Album 时,PHAssetCollection
我看不到我定义的 Smart Albums。这是我用来获取照片总数、智能相册和普通相册的代码:
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 年等。我做错了什么吗?我需要额外的价值Info.plist
吗NSPhotoLibraryUsageDescription
?这是我第一次使用 PhotoKit 框架,所以我确定我可能会误解一些东西。但是,我认为这是一个非常简单的用例。我使用 Xcode 11.5 和 Swift 在 macOS Catalina 上运行它。