Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当我将新图像上传到服务器时,如何在swift中忽略或覆盖UIImage缓存?_Ios_Swift_Caching_Uiimage - Fatal编程技术网

Ios 当我将新图像上传到服务器时,如何在swift中忽略或覆盖UIImage缓存?

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

我想在上传新图像时立即更新URL图像, 但它总是显示以前上传的图像

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与图片不同,导致您始终使用旧图片。您想要的效果是,当服务器上载新映像时,客户端可以立即显示新映像

你可以试试这些要点

  • 如果要立即显示最新图像,则必须在上载完成后通知客户端刷新页面

  • 我认为您可以尝试使用与url对应的图片,以避免处理不必要的缓存问题