[英]How should I manage correctly images provided by JSON
我正在尝试解析由我的App上下载的JSON提供的所有图像。 因此,我听说过许多正确执行此操作的方法。 我应该使用哪个API在我的应用程序上管理图像? 我应该如何做,我可以举个例子吗?
我还想正确处理以下之间的延迟:
运行应用程序->加载数据->填充UI元素
我应该怎么做才能最大程度地减少这种延迟,我认为专业的应用程序应该花这么长时间来加载所有组件。
那就是我将用图像填充UITableView的部分。
var arrCerveja = [Cerveja]()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
//TableView DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrCerveja.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! TableViewCell
let model = arrCerveja[indexPath.row]
cell.labelName.text = model.name
cell.labelDetail.text = "\(model.abv)"
cell. imageViewCell.image = ???? //How should I do that?
return cell
}
//TableView Delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
override func viewDidLoad() {
super.viewDidLoad()
getApiData { (cerveja) in
arrCerveja = cerveja
self.tableView.reloadData()
}
}
模型文件夹:
import Foundation
struct Cerveja:Decodable{
let name:String
let abv:Double
let image_url:String
}
网络文件夹:
import Alamofire
func getApiData(completion: @escaping ([Cerveja]) -> ()){
guard let urlString = URL(string: "https://api.punkapi.com/v2/beers") else {
print("URL Error")
return
}
Alamofire.request(urlString).responseJSON { response in
if response.data == response.data{
do{
let decoder = try JSONDecoder().decode([Cerveja].self, from: response.data!)
completion(decoder)
}catch{
print(error)
}
}else{print("API Response is Empty")}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.