2017-04-18 35 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

相关问题