【发布时间】:2015-10-28 16:33:59
【问题描述】:
我是 Swift 编程新手,到目前为止,我正在将 Json 数据下载为字符串并填充 UITableView。里面的图片是网址链接。问题是数据很多,有 53 个,所以也需要 53 个 url 调用来获取图像。起初,我在主线程中做所有事情,所以它滞后了很多。现在,我将执行 http 调用的代码放在异步方法中。该应用程序不会滞后,但
1) 图片没有按顺序下载(我不介意那么多,虽然这样会更好)
2) 下载速度慢,内存在 250-270mb 左右,cpu 在 40-50% 左右,而网络在 500kb/s 左右。
我没有 iPhone 来进行实际检查,但根据这些数字,我发现该应用程序使用了大量资源。我想知道为什么网络这么慢。用3g-4g我觉得应该更快更省力,不知道模拟器用的是什么。
所以我的问题是,有什么方法可以让我的应用程序运行得更快或使用更少的资源?
在将数据放在 TableView 上的代码下方。用字符串填充表格需要不到 2 秒的时间,下载所有图像需要大量时间。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "GameTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GameTableViewCell
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
cell.nameLabel?.text = games[indexPath.row].name
cell.releaseDateLabel?.text = games[indexPath.row].releaseDate
dispatch_async(backgroundQueue, {
for _ in self.games{
if let url = NSURL(string: self.games[indexPath.row].gameImage!) {
if let data = NSData(contentsOfURL: url){
cell.photoImageView?.contentMode = UIViewContentMode.ScaleAspectFit
cell.photoImageView?.image = UIImage(data: data)
}
}
}
})
return cell
}
【问题讨论】:
标签: ios json image uitableview