所以我在工作中争论什么对在 UITableView 的单元格中下载图像更好。
这两个选项是: 在 cellForRowAtIndex 中,我异步下载图像,这导致使用多个线程和多个网络请求,我们怀疑这可能是电池耗尽的问题。
另一种选择是使用一个线程和一个网络请求一次下载所有图像,方法是循环遍历一个数组,其中包含粗 cellForRowAtIndex 之外的所有图像的 URL,然后调用 TableView 重新加载函数。
后一种方法也有一个细微的修改,我们可以在下载每个图像后立即设置每个图像(可能通过调用重新加载函数)。
所以我很想知道,处理此问题的行业标准方法是什么?有哪些优点和缺点,尤其是在性能方面?有没有更好的方法?
最佳答案
我一般用SDWebImage ,它缓存图像并可以在以后重复使用。它通过其扩展和各种方法提供了灵 active 。
检查我实现的代码:
import UIKit
import SDWebImage
extension AcceptedWinksViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellIdentifier.WinkListTableViewCell) as! WinkListTableViewCell
let profile = self.winkList[indexPath.row] //Model
let placeHolderImage = UIImage(named:"placeHolder") //your placeholder image, can be class or global variable
if let url = profile.imageURL {
cell.userImageView.sd_setImage(with: url, placeholderImage: placeHolderImage, options: .refreshCached, completed: nil) //check its various options
}
else {
cell.userImageView.image = placeHolderImage
}
return cell
}
}
关于ios - 在 UITableView 的单元格中下载图像的最佳实践?苹果/swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46171644/