我有一个tableview和单元格,它有imageview,它从服务器获取图像,每当用户向下滚动到tableview结束时,tableview的数据被重新加载但我的问题是单元格图像将在每次tableview时再次从服务器下载图像重新加载,这是我的代码:
let cell = tableView.dequeueReusableCell(withIdentifier: "pizzacell", for: indexPath) as! CustomPizzaCell
let imgurl = URL(string: "\(BaseUrl)//assests/products/imgs/\(MainMenu.cellarray[indexPath.row].img)")
cell.pizzaimg.kf.setImage(with: imgurl, options: [.downloadPriority(Float(indexPath.row)),.transition(.flipFromTop(1.0)),.forceRefresh,.processor(processor)])
if indexPath.row == MainMenu.cellarray.count-1 {
if !isendoftable {
MainMenu.start += 5
MainMenu.end += 5
var parameters: Parameters = ["START": "\(MainMenu.start)"]
parameters["END"] = "\(MainMenu.end)"
parameters["TYPE"] = "\(MainMenu.type)"
print(parameters)
ApiManager.request(Address: "\(BaseUrl)/admin/Android/getLastProducts", method: .post, parameters: parameters) { json in
self.stopAnimating()
if json == nil {
let popupDialog = PopupDialog(title: "خطای دسترسی به سرور", message: "")
let btn = DefaultButton(title: "تلاش مجدد"){}
btn.titleFont = UIFont(name: "IRANSans(FaNum)", size: 15)
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
btn.titleFont = UIFont(name: "IRANSans(FaNum)", size: 17)
}
popupDialog.addButton(btn)
self.present(popupDialog, animated: true, completion: nil)
}
else {
if (json?.array?.isEmpty)! {
MainMenu.start = 0
MainMenu.end = 5
self.isendoftable = true
}
for item in (json?.array)! {
let piiiza = MainMenu.cell(id: item["id"].description, title: item["title"].description, img: item["img"].description)
MainMenu.cellarray.append(piiiza)
}
tableView.reloadData()
}
}
}
}
return cell
答案 0 :(得分:0)
您正在使用已经处理图像缓存的Kingfisher
。重新加载tableView时加载新图像应该只是读取缓存。
您需要删除此行中的.forceRefresh
:
cell.pizzaimg.kf.setImage(with: imgurl, options: [.downloadPriority(Float(indexPath.row)),.transition(.flipFromTop(1.0)),.forceRefresh,.processor(processor)])
根据文档,forceRefresh
忽略缓存并再次从URL下载。您可以看到此here。
forceRefresh
如果设置,Kingfisher将忽略缓存并尝试触发资源的下载任务。