【问题标题】:download images using alamofire - iOS使用 alamofire 下载图像 - iOS
【发布时间】:2017-01-15 16:19:23
【问题描述】:

我正在使用 Alamofire 从服务器获取数据,然后将它们放入 CarType 对象数组中,CarType 是我的结构。我从服务器得到的是nameidiconUrl。从 iconUrls 我想下载图标并将它们放在icon 中。之后,我将在集合视图中使用iconname。我的 Alamofire 请求是:

var info = [CarType]()
Alamofire.request(.GET,"url")
    .responseJSON { response in
        for (_,subJson):(String, JSON) in json["result"]
        {
            let name = subJson["name"].string
            let iconUrl = subJson["icon"].string
            let id = subJson["id"].int
            info.append(CarType(id: id!, name: name!, iconUrl: iconUrl! , image: UIImage()))
        }

我的结构是:

import Foundation
import UIKit

struct CarType {
    var name : String
    var id : Int
    var iconUrl : String
    var icon : UIImage
}

我想在 collectionView 中使用之前下载图像。 如何下载图像(使用 AlamofireImage)并将它们放入相关的 carType 图标属性中?

【问题讨论】:

    标签: ios alamofire alamofireimage


    【解决方案1】:

    您要问的是移动应用程序中的非常糟糕的做法。只是一个案例,例如,您发出一个请求并在一个数组中获得了 20 个项目,并且为了将所有 UIImages 放入您的模型中,您必须再发出 20 个请求,您甚至不知道是否您的用户最终将使用(查看)这些图标或不使用。

    相反,您可以在显示单元格时获取图像(我猜,您将在单元格中显示这些图标),为此您可以使用 SDWebImage(objective c) 或 Kingfisher 之类的库(swift),它具有 UIImageView 的扩展名,使获取和显示图像变得简单。这些库还可以缓存下载的图像。

    另外,关于对象映射的另一个建议。目前,您正在手动将json 映射到您的模型。有很多好的库可以为您处理这些问题,它们可以自动化您的对象映射过程,例如 - ObjectMapper

    希望,这很有帮助。祝你好运!

    【讨论】:

      【解决方案2】:

      我在 UITableview 中做了功能性的事情,在 CellForRowIndex 方法中添加了以下内容:

      getDataFromUrl(urlString){(data,response,error) -> Void in
                          if error == nil {
                              // Convert the downloaded data in to a UIImage object
                              let image = UIImage(data: data!)
                              // Store the image in to our cache
                              if((image) != nil){
                                  // Store the image in to our cache
                                  self.imageCacheProfile[urlString] = image
                                  // Update the cell
                                  DispatchQueue.main.async(execute: {
                                      cell.imgvwProfile?.image = image
                                  })
                              }
                              else{
                                  cell.imgvwProfile!.image = UIImage(named: "user")
                              }
                          }
                      }
      
      func getDataFromUrl(_ strUrl:String, completion: @escaping ((_ data: Data?, _ response: URLResponse?, _ error: NSError? ) -> Void)) {
          let url:URL = URL(string: strUrl)!
          let request = URLRequest(url: url)
      
          URLSession.shared.dataTask(with: request) {data, response, err in
              print("Entered the completionHandler")
              }.resume()
      
      }
      

      您还需要声明 imageCache 来存储下载的图像。

      var imageCache = [String:UIImage]()
      

      你可以在你的方法中使用上面的代码,它应该可以正常工作。

      【讨论】:

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