如何获取专辑的缩略图

Posted

技术标签:

【中文标题】如何获取专辑的缩略图【英文标题】:How to get the Album's Thumbnail 【发布时间】:2019-10-16 03:43:08 【问题描述】:

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

这就是我所拥有的:


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 正确设置了专辑标题。不知道如何获取图像并将其设置为我的图像组件。

【问题讨论】:

相册没有“图片”。 您要从相册中拍照吗?我为你看到了这个答案***.com/questions/32169185/… 【参考方案1】:

你可以试试这个从照片库中获取所有专辑名称

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))
                                            
                                        )
                                    
                                
                            
                        
                    
                
            
        

【讨论】:

以上是关于如何获取专辑的缩略图的主要内容,如果未能解决你的问题,请参考以下文章

使用 CAMERA 时从 UIImagePickerController 获取缩略图(不是专辑)

如何在 Win8 C# 应用程序中设置专辑封面?

如何从我的文件夹中获取缩略图?

如何从 facebook 获取其他人或页面视频缩略图?

Android:如何获取匹配缩略图 URI 的文件路径?

如何从文件中获取缩略图