微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决得到一个相册并在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 举报,一经查实,本站将立刻删除。