【问题标题】:how to load image in swift from URL如何从 URL 快速加载图像
【发布时间】:2018-12-29 20:04:36
【问题描述】:

我正在从 firebase 传递一个字符串格式的 URL 以加载图像,该 url 可以正常传递,但 cell.articleImage.sd_setImage(with: url, placeholderImage: UiImage(named: "issaimage")) 没有返回来自url的图片,只是占位符图片

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell") as? articlesCell else {
            return UITableViewCell()}

        let article = articleArray[indexPath.row]
        let url = URL(string: article.imageURL)!

        cell.articleImage.sd_setImage(with: url, placeholderImage: UIImage(named: "issaimage"))

        cell.configureCell(title: article.ArticleTitle, author: article.author, date: article.date)

        return cell
    }

【问题讨论】:

  • “在我的 cell.configureCell(title: article.ArticleTitle, author: article.author, date: article.date, image: image) 函数中使用未解析的标识符‘image’”
  • 显示那部分代码

标签: swift url uiimage iso


【解决方案1】:

此代码不允许我使用图像变量

let image = UIImage(data: imageData)

因为它是局部变量,如果你希望它在类中的任何地方都可以访问,它必须是实例变量

也不要使用Data(contentsOf: url),因为它会阻塞主线程,在后台队列中下载它然后在主队列中显示它或者在pod安装后使用SDWebImage

let url = URL(string: article.imageURL)!
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"))

【讨论】:

  • 如何做到这一点
  • 点击附加链接,将 pod 添加到您的 pods 文件中,如果不使用,请创建、安装,然后在 cellForRowAt 中写入上述行
  • 我安装了 pod,但由于某种原因它不允许我导入 SDWebImage
  • 从工作区打开项目,等待处理完成
  • 确保在 plist 中启用任意负载得到 http/https 请求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 2015-01-21
  • 2021-07-09
  • 1970-01-01
相关资源
最近更新 更多