简体   繁体   English

如何正确管理JSON提供的图像

[英]How should I manage correctly images provided by JSON

I'm trying to parse all the Image provided by downloaded JSON on my App. 我正在尝试解析由我的App上下载的JSON提供的所有图像。 So, I've heard many ways to do that correctly. 因此,我听说过许多正确执行此操作的方法。 Which API should I used to manage the Images on my App? 我应该使用哪个API在我的应用程序上管理图像? How should I do that, can I have an example? 我应该如何做,我可以举个例子吗?

I also wanted to take care correctly of delays between: 我还想正确处理以下之间的延迟:

Run the app --> Load data --> Populate UI elements 运行应用程序->加载数据->填充UI元素

What should I do to minimize this delay, I think a professional app shouldn't take that long to load all components. 我应该怎么做才能最大程度地减少这种延迟,我认为专业的应用程序应该花这么长时间来加载所有组件。

That's the part where I'll populate a UITableView with Images. 那就是我将用图像填充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()
        }

    }

Model Folder: 模型文件夹:

import Foundation
struct Cerveja:Decodable{
    let name:String
    let abv:Double
    let image_url:String
}

Networking Folder: 网络文件夹:

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")}

        }
}

What you can do is to cache the downloaded images, many libraries exist to help you do that, here is a list of some of them: 您可以做的是缓存下载的图像,有许多库可以帮助您完成此任务,以下是其中一些列表:

Kingfisher is a good one that also allow you to download the images and explain you how to use the library with table views. Kingfisher是一个很好的工具,它还允许您下载图像并向您说明如何使用带表视图的库。 Caching the images will also reduce the loading time the next time the app is open. 缓存图像还将减少下次打开应用程序时的加载时间。 You can also use this library to display a user friendly loading to the user during loading. 您还可以使用此库在加载期间向用户显示用户友好的加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM