ios - 在 UITableView 的单元格中下载图像的最佳实践?苹果/swift

标签 ios swift uitableview concurrency uiimageview

所以我在工作中争论什么对在 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/

相关文章:

ios - Swift 5 - 电子邮件类助手/管理器

ios - ViewController 在 iOS Simulator swift 中显示为空

ios - TableView HeaderSections 隐藏按钮

iphone - 关于 UITableView 和 deleteRowsAtIndexPaths 的问题

ios - Titanium 在浏览器窗口中使用完成按钮打开 url

objective-c - -[__ NSArrayM objectForKey:]:无法识别的选择器发送到实例Objective c中的错误

iphone - 在 Xcode 中以编程方式创建文件夹 - Objective C

android - 与 nginx 的 SSL 套接字连接在某些 android 和 ios 设备上不起作用

swift - 在swift中,使用timeIntervalSinceReferenceDate,NSDate与NSTimeInterval错误不同

objective-c - 使用 UITableView 和 GCD 减少延迟