临时UIImage从不清除

时间:2014-12-09 17:45:23

标签: ios objective-c iphone xcode swift

我需要下载一个图像并在ImageView上显示,但是在显示之后,我需要从设备中删除图像。但总是当我再次打开应用程序时,图像已经加载。

代码:

func getPhoto(pathPhoto: String, imageview: UIImageView) {

        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(20, 20, imageview.frame.size.width - 40, imageview.frame.size.height - 40))
        activityIndicator.color = UIColor(red: 64.0/255.0, green: 109.0/255.0, blue: 157.0/255.0, alpha: 1.0)
        activityIndicator.startAnimating()
        imageview.addSubview(activityIndicator)

        var photoUrlString = urlImages

        photoUrlString += pathPhoto

        var imgURL: NSURL = NSURL(string: photoUrlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
            if error == nil {
                if data == nil {
                    NSLog("Erro ao baixar")
                } else {
                    self.image = UIImage(data: data)

                    dispatch_async(dispatch_get_main_queue(), {
                        self.activityIndicator.stopAnimating()
                        self.activityIndicator.removeFromSuperview()
                        imageview.image = self.image
                    })
                }
            }
        })
    }

1 个答案:

答案 0 :(得分:2)

原因是NSURLConnection在本地自动缓存对HTTP请求的响应。因此,虽然您可能没有将图像保存在应用程序的目录中,但系统会为您保存图像。

如何处理此问题取决于您删除设备上图像的原因。如果是因为您希望每次都提供一个新的图像,并且您可以控制服务器,那么通过设置正确的HTTP标头来告知客户端不要缓存图像,这可能是有意义的。

如果您有某种数据安全原因,可以手动清除缓存,entire thingjust one request

关于NSURLCache还有一个很好的NSHipster article

相关问题