2017-04-18 36 views
1

當表格行進入視圖時,我設法獲取了一些代碼,在其中獲取Instagram圖片的位置(通過Alamofire)。但是,一旦獲取,我不希望再次調用表格行時再次進行調用。我怎樣才能避免這種行爲?當單元格在視圖中時避免再次獲取圖像

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    let url = places[indexPath.row].url 
    if let c = cell as? PlaceTableViewCell { 
     print("Downloading Image") 
     Alamofire.request(url, method: .get).responseImage { response in 
      guard let image = response.result.value else { 
       print("Error downloading image") 
       return 
      } 
      c.photoImageView.image = image 
     } 

    } 
} 

回答

3

如果您已經使用AlamoFire最簡單的方法是AlamoFireImage。它內置了可以使用的緩存機制。例如:

let imageCache = AutoPurgingImageCache() 

let urlRequest = URLRequest(url: URL(string: "https://httpbin.org/image/png")!) 
let avatarImage = UIImage(named: "avatar")!.af_imageRoundedIntoCircle() 

// Add 
imageCache.add(avatarImage, for: urlRequest, withIdentifier: "circle") 

// Fetch 
let cachedAvatarImage = imageCache.image(for: urlRequest, withIdentifier: "circle") 

// Remove 
imageCache.removeImage(for: urlRequest, withIdentifier: "circle") 

對於您的具體情況,我們可以做些簡單的事情。 AlamoFireImage包含根據您的需求量身定製的UIImageView擴展。如果它已經被下載,這應該從共享緩存中加載圖像。

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    let url = places[indexPath.row].url 
    if let c = cell as? PlaceTableViewCell { 
     print("Downloading Image") 
     c.photoImageView.af_setImage(withURL: url) 
    } 
} 

您也可以使用實用的附加功能一些可選參數,如加載圖像,而佔位符圖像或櫃面它無法加載。

c.photoImageView.af_setImage(withURL: url, placeholderImage: placeholderImage) 

或完成塊

c.photoImageView.af_setImage(withURL: url, completion: { response in 
    guard let image = response.result.value else { 
     print("Error downloading image") 
     return 
    } 
    c.photoImageView.image = image 
}) 
+1

應該不會是「https://httpbin.org/image.png」,而不是「https://httpbin.org/image/png」 – Miknash

+1

順便說一句。不知道關於AlamoFireImage,謝謝你的領導:) – Miknash

+0

你介意告訴我如何去做我做過的事情,但用AlamofireImage +緩存。我自己嘗試過一次,但由於某種原因無法啓動它 – tommyd456

1

我會建議你使用一些第三方庫(例如https://github.com/onevcat/Kingfisher)。這樣,你就可以爲你的圖像緩存,而且將來不必再次下載它。

但是,如果您不想用更多的第三方庫進行喧囂,您可以創建一系列圖像,並在下載之後將其放入數組中。實例化與您的數據陣列大小相同的數組。然後,你的代碼看起來像:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

    let url = places[indexPath.row].url 
    if let c = cell as? PlaceTableViewCell { 
     print("Downloading Image") 
     if let image = arrayOfImages[indexPath.row] { 
      c.photoImageView.image = image 
     } 
     else { 
      Alamofire.request(url, method: .get).responseImage { response in 
       guard let image = response.result.value else { 
        print("Error downloading image") 
        return 
       } 
       c.photoImageView.image = image 
       self.arrayOfImages[indexPath.row] = image 
      } 
     } 
    } 
} 
+0

我喜歡這個建議。謝謝 – tommyd456

+0

我想缺點是沒有緩存,所有圖像需要在應用程序加載時重新檢索? – tommyd456

+0

這是正確的 - 當你回到屏幕時你會再次下載它們。這是更臨時的緩存,然後是臨時的一個 – Miknash

相關問題