得到一个相册并在Swift IOS中显示

问题描述

我是Swift的初学者,我想显示相册而不是显示iPhone上的每张照片。

因此,首先,我尝试显示我拥有的所有照片,并尝试将代码更改为从相册获取信息,但我并没有真正了解如何操作。请使用PHFetchResult帮助我

这是我的代码

class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!

var fetchResults: PHFetchResult<PHAssetCollection>!

let imageManager: PHCachingImageManager = PHCachingImageManager()
var assetCollection: PHAssetCollection!

override func viewDidLoad() {
    super.viewDidLoad()
    authorizationStatus() /* is just to check whether it's authorized or not and if yes,call func createalbum()*/
}

func createalbum(){
    let fetchOptions = PHFetchOptions()
    fetchOptions.predicate = nspredicate(format: "title = %@","MyAlbum")
    let collection : PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .smartAlbum,subtype: .any,options: fetchOptions)
    assetCollection = collection.firstObject
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
  //  self.fetchResults = PHAssetCollection.fetchCollections(in: albumCollection,options: fetchOptions)
}

这是我的另一个代码

extension ViewController: UICollectionViewDataSource {
//howmany
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
    return self.fetchResults?.count ?? 0
}
//how
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionViewCell",for: indexPath) as? AlbumCollectionViewCell else {
        return UICollectionViewCell()
    }
    let asset: PHAsset = fetchResult.object(at: indexPath.item)
    
    imageManager.requestimage(for: asset,targetSize: CGSize(width: 180,height: 240),contentMode: .aspectFill,options: nil,resultHandler: { image,_ in
                                cell.albumImage.image = image
                                
    })
    return cell
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)