【问题标题】:Why am I getting error at loading image from JSON to tableview using kingfisher?为什么我在使用 kingfisher 将图像从 JSON 加载到 tableview 时出错?
【发布时间】:2020-11-28 10:52:58
【问题描述】:

我正在尝试借助 Kingfisher 库将从 JSON 响应中获得的图像加载到 tableView 中。 这是从 JSON 响应中获得的图像:

"image": "/static/restaurants/image-1593600873191.jpg"

现在我在 cellForRow 方法中编写这段代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.lblName.text = arrUsers[0].data?.categories[0].name
        let url = URL(string:arrUsers[0].data?.bestOffers[0].image ?? "No Url")
       cell.imgName.kf.setImage(with: url)
        return cell
      }

在上面的代码中,我将图像的 JSON 响应转换为 URL,然后将其传递给 setImage 方法。

标签被设置为tableView,但图像未加载到tableViewCell,我在控制台中收到此错误。 please find attached the screenshot for refernce.

【问题讨论】:

  • /static/restaurants/ - 是在您的应用或资产目录中创建的目录吗?
  • @Satheesh 它是资产目录,因为我刚刚收到这个值作为 json 响应。
  • " assets directory" 在您的应用程序中,还是使用远程 url?您需要将基本网址放在前面。

标签: ios json swift imageview tableview


【解决方案1】:

改变这一行:

let url = URL(string:arrUsers[0].data?.bestOffers[0].image ?? "No Url")

let url = URL(string: "https://yourwebsiteaddress" + (arrUsers[0].data?.bestOffers[0].image ?? "No Url"))

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多