UIImage Temporary

0

I need to download an image, it is displayed, however after displaying it I need to delete it from the device. But whenever I open the application again it is already loaded.

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
                    })
                }
            }
        })
    }
    
asked by anonymous 09.12.2014 / 16:46

1 answer

0

The reason is NSURLConnection automatically caches responses to HTTP requests locally. So while you can not be saving the image in the application directory, iOS is saving for me.

How you deal with this depends on why you are deleting the image on the device. If it is because you want to serve a new image at a time, and you have control over the server, it may make sense to fix it there by setting appropriate HTTP headers to inform clients not to cache the image.

To solve this problem I have added the following code snippet after downloading the image:

NSURLCache().removeCachedResponseForRequest(request)

It looks like this:

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

                            NSURLCache().removeCachedResponseForRequest(request)
                        })
                }
            }
        })
    }
    
10.12.2014 / 16:36