【发布时间】:2020-11-24 21:15:35
【问题描述】:
目标是从目录中获取图像名称并将它们添加到 UIImages 数组中。
var photoArray = [UIImage]()
func getImageFromDocumentDirectory() -> [UIImage] {
let fileManager = FileManager.default
var imageNames = [String]()
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0] as NSString).appendingPathComponent("DIRECTORYNAME")
do {
let items = try fileManager.contentsOfDirectory(atPath: imagePath)
for item in items {
这就是我遇到问题的地方:错误:找到 nil (let images)
let images = UIImage(contentsOfFile: item)
photoArray.append(images!)
}
} catch {
print(error.localizedDescription)
}
return photoArray
}
将 func 添加到集合视图以提取图像。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)
-> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL",
for: indexPath) as! CELL
let images = getImageFromDocumentDirectory()
// photoImageView is a UIImageView in the cell.
cell.photoImageView.image = images[indexPath.row]
}
【问题讨论】:
标签: arrays swift null uicollectionview