【问题标题】:Swift 4 proper way to display images from api in UITableViewSwift 4 在 UITableView 中显示来自 api 的图像的正确方法
【发布时间】:2018-08-19 14:10:33
【问题描述】:

我正在使用 UITableViewController 显示来自网络服务的项目列表,这些项目有图像,我使用 AlamofireImage 从图像中获取数据并将它们显示在 UITableViewCell 中,如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "postsCell", for: indexPath) as! PostsCell

        let imgUrl = URL(string:"http://example.com/uploads/" + (self.array[indexPath.row]["cover"] as! String))

        cell.delegate = self

        Alamofire.request(imgUrl!).responseImage { response in
            DispatchQueue.main.async {
                if let image = response.result.value {
                    cell.message?.text = (self.array[indexPath.row]["title"] as! String)
                    cell.subMessage?.text = (self.array[indexPath.row]["username"] as! String)
                    cell.profileImage?.image = image

                }
            }
        }

        return cell
    }

这就是我如何填充 self.array

getPosts(username: self.username!) { result in

            self.array = result
            self.tableView.reloadData()

        }

这里是getPosts:

func getPosts(username: String, completionHandler:@escaping (_ result:Array<Dictionary<String, Any>>) -> Void)
    {
        var returnedResults = Array<Dictionary<String, Any>>()

        APIController().getUsersPosts(username: username)
        {
            (result: Array<Dictionary<String, Any>>) in

            DispatchQueue.main.async {

                //Return our results

                returnedResults = result
                completionHandler(returnedResults)

            }
        }
    }

这是我对我的 api 的调用

func getPosts(username: String, completion: @escaping (_ result: Array<Dictionary<String, Any>>) -> Void)
    {

        let parameters: Parameters = [
            "username": username
        ]

        Alamofire.request(webservice + "?action=posts", method: HTTPMethod.post, parameters: parameters, encoding: URLEncoding.httpBody, headers: [:]).responseJSON { response in

            if(response.error == nil)
            {
                if let result = response.result.value {

                    let jsonData = result as! Array<Dictionary<String, Any>>

                    completion(jsonData)

                }
            }

        }
    }

我的问题是,有没有更好更有效的方式来显示来自 URL 的图像?

【问题讨论】:

  • 您可以使用 Alamofire 提供的缓存服务来缩短加载时间。

标签: ios swift alamofire


【解决方案1】:

您可以使用 SDWebImage 为您的应用维护图像缓存,

您可以通过以下方式使用它:

import SDWebImage

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postsCell", for: indexPath) as! PostsCell

    let imgUrl = URL(string:"http://example.com/uploads/" + (self.array[indexPath.row]["cover"] as! String))

    cell.profileImage.sd_setImage(with: imgUrl, placeholderImage: UIImage(named: "placeholder.png"))

    // REST OF YOUR CODE TO FILL OTHE DATA OF YOUR CELL
    return cell
}

您可以参考下面的 git 链接了解更多信息: https://github.com/rs/SDWebImage

【讨论】:

    【解决方案2】:

    不需要写responseImagecompletionHandler 你可以像下面这样:

    cell.profileImage?.af_setImage(withURL: imgUrl) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多