ANDYNVT
ANDYNVT

Reputation: 671

How to fetch "Camera Roll", "Videos", "Favorites", "Selfies" and "Screenshots" in fetch PHAssetCollection statement - swift 4

I am working with Photo framework and try to fetch all photos & videos in all albums on iPhone. But the problem is I can't fetch all albums including "Camera Roll", "Videos", "Favorites", "Selfies", "Screenshots" and others albums with the fetchAssetCollections statement. I have only fetched others albums.

I have tried all of the album and subtype, but it doesn't help me.

Here is my code:

var collections: PHFetchResult<PHAssetCollection>!

@objc private func loadPhoto(notification: Notification) {
        if let object = notification.object {
            photoArray.removeAll()

            collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

            collections.enumerateObjects { (collection, _, _) in
                let photoInAlbum = PHAsset.fetchAssets(in: collection, options: nil)

                photoInAlbum.enumerateObjects({ (asset, _, _) in
                    let imageSize = CGSize(width: 200, height: 200)
                    let photoOption = PHImageRequestOptions()
                    photoOption.deliveryMode = .fastFormat
                    photoOption.isSynchronous = true

                    PHCachingImageManager.default().requestImage(for: asset, targetSize: imageSize, contentMode: .aspectFill, options: photoOption, resultHandler: { (image, info) in
                        self.photoArray.append(image!)
                    })
                })
            }
            listPhotoView.reloadData()
        }
    }

I expect the output is all the albums including "Camera Roll", "Videos", "Favorites", "Selfies", "Screenshots" and others albums.

Upvotes: 1

Views: 1729

Answers (1)

holtmann
holtmann

Reputation: 6303

Change

collections = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

to

collections = PHAssetCollection.fetchAssetCollections(with: . smartAlbum, subtype: .any, options: nil)

All albums you want to list are "Smart Albums". .album refers to user created albums.

Upvotes: 2

Related Questions