2017-08-11 107 views
0

我需要AlamofireImages一点帮助。在应用中,轻敲表视图细胞会下载并设置使用内置的ImageView的扩展,下面看到的图像:缓存AlamofireImage响应磁盘

func sendImageRequest(imageView: UIImageView, item: CatalogItem, isLargeImage: Bool? = false){ 
    let imageURL = ImageManager.URLBuilder(sku: item.sku, largeImage: isLargeImage) 

    // Clear the image so that recycled images are cleared. 
    imageView.image = nil 

    // Create the request and add the token string to the authorization header 
    var urlRequest = try! URLRequest(url: URL(string: imageURL)!) 
    urlRequest.setValue("Bearer \(ImageManager.getTokenString()!)", forHTTPHeaderField: "Authorization") 

    // If the token exists AND is VALID (not expired) - Get the image. 
    if ImageManager.checkIfTokenExists() && ImageManager.checkTokenIsValid(){ 
     debugPrint("Valid token already exists. Fetching image... TableViewExtension") 
     imageView.af_setImage(withURLRequest: urlRequest){ response in 
      switch response.result{ 
      case .success(let image): 
       debugPrint(response) 
       debugPrint("SUCCESS") 
       break 
      case .failure(let error): 
       debugPrint("Error: \(error)") 
       debugPrint(response.response) 
       break 
      } 
     } 
    } 
    // Else if the token exists AND is INVALID (expired) - Delete the old token, get a new one, and fetch the image 
    else if ImageManager.checkIfTokenExists() && !ImageManager.checkTokenIsValid(){ 
     debugPrint("Token expired... Getting new token.") 
     ImageManager.deleteToken() 
     let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io") 
     tokenRequest.requestJWTToken(){ 
      debugPrint("Token renewed. Fetching image...") 
      imageView.af_setImage(withURLRequest: urlRequest) 
     } 
    } 
    // If the token doesn't exist, request a new one and fetch the image. 
    else{ 
     debugPrint("Requesting new token...") 
     let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io") 
     tokenRequest.requestJWTToken(){ 
      debugPrint("Token aquired. Fetching image...") 
      imageView.af_setImage(withURLRequest: urlRequest) 
     } 
    } 
} 

所以,这部分我没事用。它的工作原理是,将对象缓存到磁盘并设置ImageView图像。如果稍后再次点击它,它会检查图像是否在缓存中,如果是,只需应用图像。整齐。

现在,项目的一个新要求是让用户能够预先载入每个图像并缓存它们。现在我不再使用AFI ImageView扩展,而是使用Downloader。我不确定如何让Downloader到: A)缓存到磁盘和 B)当用户点击一个单元格时,让该ImageView扩展程序检查相同的缓存以查看它是否预加载并应用该图像。

AFI在发送请求之前足够聪明地检查缓存中的图像数据,所以我确信这是可能的。

我至今下载下面的图片是:

static var downloader = ImageDownloader.default 

static func downloadImage(urlRequest: URLRequestConvertible) 
{ 
    self.downloader.download(urlRequest) { response in 
     switch response.result{ 
     case .success(let image): // If successful, cache the image. 
      print("SUCCESS") 
      break 
     case .failure(let error): // If unsuccessful, remove the cached 404 response and apply a nil image. 
      print("ERROR: \(response.error)") 
      break 
     } 
    } 
} 

如果任何人有任何想法如何做到这一点或任何有用的提示或技巧,这将是伟大的!谢谢!

回答

0

嗯,我假设你正在从字符串下载图像作为下载网址。我已经处理了同样的问题。在我的情况,我没有使用Alamofire,我使用的扩展图像视图:

let imageCache = NSCache<AnyObject, AnyObject>() 

扩展的UIImageView {

func downloadImage(from imgURL: String!) { 

    self.image = nil 

    // Check if there's an image in the cache 

    if let cachedImage = imageCache.object(forKey: imgURL as AnyObject) as? UIImage { 
     self.image = cachedImage 
     return 
    } 

    // Otherwise, download image from Firebase via URL-string 

    let url = URLRequest(url: URL(string: imgURL)!) 

    let task = URLSession.shared.dataTask(with: url) { 
     (data, response, error) in 

     if error != nil { 
      print(error!) 
      return 
     } 

     DispatchQueue.main.async { 
      if let image = UIImage(data: data!) { 
       imageCache.setObject(image, forKey: imgURL as AnyObject) 
      } 
      self.image = UIImage(data: data!) 
     } 

    } 

    task.resume() 
} 

}

现在我解决了这个问题,预紧力通过只需拨打

for i in objects { // whatever object you're having 

    imageView.downloadImage(fromURL: i.url) 

} 

...在viewDidLoad()中。

现在它遍历所有自定义对象下载和缓存所有图像,并且如果用户点击图像,它会立即从缓存中加载。我希望这能帮助你解决你的问题。