ios - 从 api 获取 gif 图像并显示到 tableview 中
问题描述
如果有人有好的想法来实现这个让我知道,谢谢
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewCell: UITableViewCell!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "trendingCell", for: indexPath)
print(self.array[indexPath.row])
cell.textLabel?.text = (self.array[indexPath.row]["title"] as! String)
cell.detailTextLabel?.text = (self.array[indexPath.row]["username"] as! String)
Alamofire.request(imageUrl!, method: .get).response { response in
guard let image = UIImage(data:response.data!) else {
// Handle error
return
}
let imageData = UIImageJPEGRepresentation(image,1.0)
cell.myImage.image = UIImage(data : imageData!)
}
return cell
}
}
解决方案
您正在强制解开一些值。尝试安全地解开这些值。不过,您仍然必须弄清楚为什么这些值为零。
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
if let url = NSURL(string: self.restaurants[indexPath.row].restaurantImage), let data = NSData(contentsOfURL: url), let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
cell.restaurantImage.image = image
}
}
})
推荐阅读
- python - Neo4j - 如何在 python 中使用 neo4j-driver 在另一个查询中使用密码查询的结果
- mysql - 用presto如何显示像XXX这样的表格
- mysql - 两侧的唯一键?避免 (1,2) (2,1) 也
- mysql - 将 mysql 8 与 django 连接时出错
- python - 如何在 python 中解析非常大的文件?
- cordova - Cordova 为插件合并文件夹
- excel - Excel 中的矩阵是否填充了表格中的数据?
- vb.net - 索引数超过索引数组 vb 的维数
- java - 如何获取 Feign 客户端的名称?
- tensorflow-lite - 如何训练 mobilenet-v1 量化模型以在 TF Lite 上使用