gpt4 book ai didi

ios - 如何在新搜索中取消 uitableviewcells 的异步图像加载

转载 作者:行者123 更新时间:2023-11-30 11:54:11 26 4
gpt4 key购买 nike

我开始在我的服务器上搜索用户的个人资料。当用户输入用户名时,UITableView 中的单元格中会出现不同的建议配置文件。每个配置文件都有一个与之关联的图像。一旦用户键入,图像就会通过 cellForItem(at:) 函数异步加载。当用户键入搜索时,它会取消我对服务器的搜索请求,但它还需要取消异步请求。一次可能会发生多个请求 - 我应该如何跟踪它们?

我下载图像的代码如下:

model.downloadImage(
withURL: urlString,
completion: { [weak self] error, image in
guard let strongSelf = self else { return }
if error == nil, let theImage = image {
// Store the image in to our cache
strongSelf.model.imageCache[urlString] = theImage
// Update the cell with our image
if let cell = strongSelf.tableView.cellForRow(at: indexPath) as? MyTableCell {
cell.profileImage.image = theImage
}
} else {
print("Error downloading image: \(error?.localizedDescription)")
}
}
)

请注意,如果我愿意,我可以添加 request = model.downloadImage(... ,但 request 只会保存指向上次 downloadImage() 函数被调用。

最佳答案

为此目的,我建议使用 Kingfisher 。它是强大的第三方库,用于下载和缓存图像。使用翠鸟,您只需调用即可实现您想要的:

// In table view delegate
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//...
cell.imageView.kf.cancelDownloadTask()
}

关于ios - 如何在新搜索中取消 uitableviewcells 的异步图像加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48011594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com