【发布时间】:2018-08-21 03:20:11
【问题描述】:
我有问题。
我想从 url 获取图像,并在表格单元格中显示 UIimage。
let url 有价值,但它变成了nil
运行这段代码出错了
Fatal error: Unexpectedly found nil while unwrapping an Optional value
为什么值为nil?
let url = URL(string: stores![indexPath.row].photos[0].path)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
cell.imageCell.image = UIImage(data: data!)
}
}
return cell
}
商店![indexPath.row].photos[0].path 值
http://127.0.0.1:8000/photos/NY_0.jpeg
【问题讨论】:
-
不要对非本地资源 URL 使用 Data(contentsOf: URL) 初始化程序。你不应该同步下载这些。
-
127.0.0.1:8000/photos/NY_0.jpeg 看起来不是一个有效的网址
-
代码似乎应该可以工作。但是,您使用本地 API 来获取图像,
Check if AppTransportSecurity is allowing you to make the request。 -
@kathayatnk 我设置了
AppTransportSecurity但值为零 -
你确定
stores![indexPath.row].photos[0].path确实有http://127.0.0.1:8000/photos/NY_0.jpeg的准确值吗?这是一个完全有效的 URL,并且 not 会导致url成为nil。确保值中没有空格。
标签: ios json swift api uiimage