【问题标题】:UIImageView in cell not showing image单元格中的 UIImageView 不显示图像
【发布时间】: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文件,所以要转成图片。

标签: ios swift


【解决方案1】:

pbb 你没有正确保存图片。检查一下。

【讨论】:

  • 它们已正确保存,我已尝试打印 URL。
  • @abcd 我的意思是图片二进制文件,而不仅仅是网址
  • 如何检查?
【解决方案2】:

解决了:

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)
    if let image1 = mainImage {
        mainImageView.image = image1
    }
    return cell!
}

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多