MacOS PhotoKit - List all user-created Smart Albums?
NickName:Shadowman Ask DateTime:2020-05-24T04:44:59

MacOS PhotoKit - List all user-created Smart Albums?

I've started playing around with macOS development for the first time. I'm trying to query the list of Smart Albums I've created in the macOS Photos app. As you can see in this screenshot, I've defined 18 Smart Albums and 1 regular Album.

Screenshot of the Albums

However, when I fetch the Smart Album PHAssetCollections I do not see the Smart Albums I've defined. Here is the code I'm using to fetch a total photo count, the Smart Albums, and then normal 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))")
}

However, when I run this code I see the following output, showing that my user-created Smart Albums are not being returned:

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

As you can see, my normal album My Test Album is returned, but none of the Smart Albums I created, such as those for 2008, 2009, etc. Am I doing something wrong? Do I need an additional value in my Info.plist beyond including NSPhotoLibraryUsageDescription? This is my first time playing around with the PhotoKit framework so I'm sure I may be misunderstanding something. But, I thought this was a pretty simple use case. I'm running this on macOS Catalina, using Xcode 11.5 and Swift.

Copyright Notice:Content Author:「Shadowman」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61978366/macos-photokit-list-all-user-created-smart-albums

More about “MacOS PhotoKit - List all user-created Smart Albums?” related questions

MacOS PhotoKit - List all user-created Smart Albums?

I've started playing around with macOS development for the first time. I'm trying to query the list of Smart Albums I've created in the macOS Photos app. As you can see in this screenshot, I've d...

Show Detail

iOS PhotoKit: Fetch all smart albums except panoramas

I am using the follow code to fetch all smart albums: PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.albumRegular, options: nil) ...

Show Detail

Sorting Shared Albums with the Apple IOS Photokit

Before I do a lot of fruitless experimenting, does anyone know if you can sort shared albums using the Apple Photokit. I know the native photos app is not able to.

Show Detail

Xcode: how to access shared photo albums in MacOS 10.10

I'm using the Media library for identifier: "com.apple.Photos.SharedGroup" and I'm able to parse thru all my shared photo albums by adding observers using the observeValueForKeyPath method to extract

Show Detail

How to load assets from smart album? (expo-media-library)

Is there a way to load assets from smart album (on iOS) using expo-media-library? I've tried to load assets by following instructions from documentation (Selfies is a name of smart album): const al...

Show Detail

iOS8 PhotoKit, some albums can't be finded

When I used PhotoKit to create my albums,there is not all albums in my project,some albums imported from computer can't be searched.I will post my code follow. //get system albums PHImageRequestOp...

Show Detail

Fetching non-empty Smart Albums

Using Swift4 / ios11, I am trying to fetch a collection of non-empty smart albums from a device: let smartAlbumOptions = PHFetchOptions() smartAlbumOptions.predicate = NSPredicate(format: "

Show Detail

How to list Shared Photo Stream Albums with PhotoKit

I can obtain a list of Photo Albums created on the device with PHFetchResult *results = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil]; I imagine there must be a similar way to obt...

Show Detail

Expo MediaLibrary ios smart albums not returning

i try to het all album names via EXPO MediaLibrary, however MediaLibrary.getAlbumsAsync() only returns user created albums. Can anyone please suggest how you handled this situation and get ios smart

Show Detail

Formatting NSPredicate for Photokit Fetch

I'm having a lot of trouble understanding how to formulate filers for photokit queries, specifically, how to formulate an NSPredicate when you only want to retrieve certain mediaTypes. In my case, ...

Show Detail