2017-08-11 178 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()中。

現在它遍歷所有自定義對象下載和緩存所有圖像,並且如果用戶點擊圖像,它會立即從緩存中加載。我希望這能幫助你解決你的問題。