【发布时间】:2017-02-02 21:23:50
【问题描述】:
所以我有一个表格视图和一个名为 offlineImages 的数组,其中包含存储在 Documents 文件夹中的图像。我希望这些图像显示在单元格的图像视图中。代码编译,但不显示图像。存储的 URL、单元标识符和标签是正确的。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "offlineCell")
let mainImageView = cell?.viewWithTag(2) as! UIImageView
let mainImageURL = offlineImages[indexPath.row]
let mainImageData = NSData(contentsOf: mainImageURL)
let mainImage = UIImage(data: mainImageData! as Data)
mainImageView.image = mainImage
return cell!
}
这是我下载图像的方式:
let imageDestination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent("image."+"png")
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(mainImageURL, to: imageDestination).response { response in
if response.destinationURL != nil {
finalImageURL = response.destinationURL!
}
}
【问题讨论】:
-
UIImage(data:)返回一个可选值。你检查它是否为零? -
不是零。网址正确。
-
更好地说,它不应该。实际上,我已经检查过,它们是零。为什么?
-
因为不能保证任意的Data对象都能变成图片
-
图片是从网上下载的,在Documents文件夹中保存为.jpg文件,所以要转成图片。