如何获取相册的缩略图

时间:2019-10-16 03:43:08

标签: ios swift

我正在尝试在设备上显示相册的收藏夹视图。我可以获取标题,但不确定如何获取相册照片并设置为该单元格。

这就是我所拥有的:


import UIKit
import Photos

class PhotoAlbumViewController: UICollectionViewController {

    var albumList: PHFetchResult<PHAssetCollection>! = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        albumList = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        albumList.count
    }

    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let album = albumList.object(at: indexPath.row)

        if let label = cell.viewWithTag(4000) as? UILabel {
            label.text = album.localizedTitle
        }
    }

}

我的故事板上有一个UI标签,并且label.text = album.localizedTitle正确设置了相册标题。不确定如何获取图像并将其设置为我的图像组件。

1 个答案:

答案 0 :(得分:0)

您可以尝试从照片库中获取所有相册名称

func get_All_Albums()
    {
        DispatchQueue.global(qos: .userInteractive).async
        {
            let fetchOptions = PHFetchOptions()
            fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

            let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
            let customAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

            [smartAlbums, customAlbums].forEach {
                $0.enumerateObjects { collection, index, stop in

                    let requestOptions = PHImageRequestOptions()
                    requestOptions.isSynchronous = true
                    requestOptions.deliveryMode = .highQualityFormat

                    let photoInAlbum = PHAsset.fetchAssets(in: collection, options: fetchOptions)

                    if let title = collection.localizedTitle
                    {
                        if photoInAlbum.count > 0
                        {
                            print("\n\n \(title) --- count = \(photoInAlbum.count) \n\n")
                        }
                    }
                }
            }
        }
    }

&尝试通过此操作从特定相册获取照片

    func get_Photos_From_Album(albumName: String)
        {
            var photoLibraryImages = [UIImage]()
            var photoLibraryAssets = [PHAsset]()
            //whatever you need, you can use UIImage or PHAsset to photos in UICollectionView

            DispatchQueue.global(qos: .userInteractive).async
            {
                let fetchOptions = PHFetchOptions()
                fetchOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

                let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: nil)
                let customAlbums = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)

                [smartAlbums, customAlbums].forEach {
                    $0.enumerateObjects { collection, index, stop in

                        let imgManager = PHImageManager.default()

                        let requestOptions = PHImageRequestOptions()
                        requestOptions.isSynchronous = true
                        requestOptions.deliveryMode = .highQualityFormat

                        let photoInAlbum = PHAsset.fetchAssets(in: collection, options: fetchOptions)

                        if let title = collection.localizedTitle
                        {
                            if photoInAlbum.count > 0
                            {
                                print("\n\n \(title) --- count = \(photoInAlbum.count) \n\n")
                            }

                            if title == albumName
                            {
                                if photoInAlbum.count > 0
                                {
                                    for i in (0..<photoInAlbum.count).reversed()
                                    {
                                        imgManager.requestImage(for: photoInAlbum.object(at: i) as PHAsset , targetSize: CGSize(width: 150, height: 150), contentMode: .aspectFit, options: requestOptions, resultHandler: {
                                            image, error in
                                            if image != nil
                                            {
                                                photoLibraryImages.append(image!)
 photoLibraryAssets.append(photoInAlbum.object(at: i))
                                            }
                                        })
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }