Uiimage Temporaria

Asked

Viewed 58 times

0

I need to download an image, it is displayed, but after displaying it I need to delete it from the device. But always when I open the app 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
                    })
                }
            }
        })
    }
  • Please add information on how you download the image and how it is loaded.

  • At no time in your code the image is being saved on the device. Be sure that this is the code snippet same?

  • Exactly, why I wanted to know how to delete the image that was being saved temporarily. Below follows the solution as answer I found.

1 answer

0


The reason is that NSURLConnection automatically caches responses to HTTP requests locally. So while you can’t be saving the image in the app directory, iOS is saving it for me.

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

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

NSURLCache().removeCachedResponseForRequest(request)

Staying 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)
                        })
                }
            }
        })
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.