【发布时间】:2021-06-13 00:30:18
【问题描述】:
我正在尝试创建一个 UICollectionViewController,就像 iPhone 上的“照片”应用中的那个。
这是我正在使用的代码:
import UIKit
import Photos
private let reuseIdentifier = "dataCell"
class GalleryCollectionViewController: UICollectionViewController {
var images = [PHAsset]()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(GalleryImageCell.self, forCellWithReuseIdentifier: reuseIdentifier)
getImages()
// Do any additional setup after loading the view.
}
func getImages() {
let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
assets.enumerateObjects({ (object, count, stop) in
// self.cameraAssets.add(object)
self.images.append(object)
})
self.images.reverse()
self.collectionView!.reloadData()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return images.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! GalleryImageCell
let asset = images[indexPath.row]
let ImageManager = PHImageManager.default()
if cell.tag != 0 {
ImageManager.cancelImageRequest(PHImageRequestID(cell.tag))
}
cell.tag = Int(ImageManager.requestImage(for: asset, targetSize: CGSize(width: 120, height: 120), contentMode: .aspectFill, options: nil, resultHandler: { (result, _) in
cell.galleryImage?.image = result
}))
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.width * 0.32
let height = self.view.frame.height * 0.179910045
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2.5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
无论如何,当我加载视图时,我只会看到黑色视图。即使在视图滚动很长时间时似乎创建了单元格,图像也根本不显示。
这不是与隐私权限相关的问题,因为我已经授予对“所有照片”的访问权限,并且images 包含所有照片资产。
你知道我做错了什么吗? 提前谢谢你
【问题讨论】:
标签: uicollectionview uicollectionviewcell swift5 phasset photokit