【问题标题】:How to assign value of type NSURL to type UIimage?如何将 NSURL 类型的值分配给 UIimage 类型?
【发布时间】:2016-12-30 16:28:55
【问题描述】:

您好,我有一个 Json 文件:

{
   "Name":"Car",
   "Picture":"http://www.starpropertiesindia.com/blog/wp-content/uploads/2016/08/kochi1.jpg",
   "Description":"Ford"
}

在 Table View Controller 中读取名称图片和描述。

我的 TableViewController 中有这段代码:

let name = aFruit["Name"].stringValue
let imageURL = aFruit["Picture"].stringValue
let description = aFruit["Description"].stringValue

现在我想将它们返回到表格视图单元格中。对于这种情况,我使用以下代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as!  TableViewCell
    let fruit = fruits[indexPath.row]
    cell.CellTitle.text = fruit.name
    cell.CellDescription.text = fruit.description
    cell.CellImage.image = fruit.imageURL
    return cell
}

一切都适合

cell.CellTitle.text = fruit.name

cell.CellDescription.text = fruit.description

但是当我写的时候

cell.CellImage.image = fruit.imageURL

它给了我一个错误:

无法将 NSURL 类型的值赋给 UIimage 类型!

这里可能有什么错误?

【问题讨论】:

  • 您的网络调用在哪里获取图像?
  • 错误明确指出,.image 需要 UIImage 实例而不是 URL ... U 可以调用异步请求以延迟加载图像 ...
  • 你能给我举个例子吗?我是初学者,我不明白

标签: json swift uiimage nsurl


【解决方案1】:

试试这样的...我还没有测试过。

let image = UIImage(data: NSData(contentsOf: NSURL(string: fruit.imageURL)))

【讨论】:

  • @Dakata“不工作”在您编程时不是一个很好的描述......您是否遇到错误或崩溃?请更具体。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多