【发布时间】:2017-06-08 10:25:06
【问题描述】:
我从 json 下载图像链接,然后在表格视图开始创建其单元格后创建图像:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
DispatchQueue.main.async(execute: { () -> Void in
if let url = NSURL(string: self.movies[indexPath.row].image)
{
if let data = NSData(contentsOf: url as URL)
{
let imageAux = UIImage((data: data as Data))
cell.movieImage.image = imageAux
self.tableView.reloadData()
}
}
})
cell.name = self.movies[indexPath.row].name
cell.date = self.movies[indexPath.row].date
return cell
}
这很好用,但是表格视图变得非常慢,不是在渲染时而是在滚动时。我一直在检查 RAM 和 CPU,两者都非常低,但我的网络使用量一直在上升,但是图像已经在单元格上,所以这意味着它已经完成了。 (对于这个测试,我只为 2 部电影调用 JSON,所以 2 张图片)
在我开始这样做之前,我的总下载量约为 200kb(带图像),现在在我停止项目之前已经超过 2MB。
我做错了什么?
【问题讨论】:
标签: ios json swift asynchronous tableviewcell