Ios 当我将新图像上传到服务器时,如何在swift中忽略或覆盖UIImage缓存?
我想在上传新图像时立即更新URL图像, 但它总是显示以前上传的图像Ios 当我将新图像上传到服务器时,如何在swift中忽略或覆盖UIImage缓存?,ios,swift,caching,uiimage,Ios,Swift,Caching,Uiimage,我想在上传新图像时立即更新URL图像, 但它总是显示以前上传的图像 func changeUserIMG(imgURL:String){ if let url = URL(string: imgURL) { URLSession.shared.dataTask(with: url, completionHandler: {(data,responds,error) in if error != nil{ prin
func changeUserIMG(imgURL:String){
if let url = URL(string: imgURL) {
URLSession.shared.dataTask(with: url, completionHandler: {(data,responds,error) in
if error != nil{
print(error!.localizedDescription)
}
else if let imageData = data{
DispatchQueue.main.async {
self.userImage.image = UIImage(data: imageData)
}
}
}).resume()
}
}
是否仍有覆盖或忽略UIImage chache的方法
编辑:
即使我尝试使用.reloadIgnoringLocalCacheData,仍然会显示以前上载的图像
func changeUserIMG(imgURL:String){
if let url = URL(string: imgURL) {
URLSession.shared.dataTask(with: url, completionHandler: {(data,responds,error) in
if error != nil{
print(error!.localizedDescription)
}
else if let imageData = data{
DispatchQueue.main.async {
self.userImage.image = UIImage(data: imageData)
}
}
}).resume()
}
}
问题出在哪里?我找到了一些清除URLSession缓存的方法: 1) 将
URLSession.shared
替换为URLSession(配置:URLSessionConfiguration.ephemeral)
2) 在重新加载数据之前添加此方法:
URLCache.shared.removeAllCachedResponses()
结合您的问题,相同的url与图片不同,导致您始终使用旧图片。您想要的效果是,当服务器上载新映像时,客户端可以立即显示新映像
你可以试试这些要点