I upload pictures from the server through this method:

func imgfromlink(link url: String) -> UIImage? { if let iurl = NSURL(string: url) { if let idata = NSData(contentsOfURL: iurl) { if let image = UIImage(data: idata){ return image } } } return nil } 

The essence is this: in the method I pass a link to the picture, and at the output I get a UIImage. But I have a little problem. If the image on the server is updated (the URL of the image remains the same), and I try to download this image again through this method, then it is not updated.

I thought it was probably caching and tried to pick NSData and found the following options: .DataReadingUncached key there options: .DataReadingUncached

I redid my method:

  func imgfromlinkUncached(link url: String) -> UIImage? { if let iurl = NSURL(string: url) { do { let idata = try NSData(contentsOfURL: iurl, options: .DataReadingUncached) if let image = UIImage(data: idata){ return image } } catch _ { return nil } } return nil } 

But nothing has changed. Pictures are not updated. And for me this is critical, since the user should be able to change the avatar and everywhere should be displayed the updated version of the avatar, but the url of the avatar remains unchanged, only the picture on the server changes.

Xcode version 8.0

  • one
    Can you please provide more code? Perhaps the error is not in this function, but for example in the fact that you are not updating the variable storing the result of the execution of this function. - Roman Podymov
  • one
    Oftop, but I still have to say this - you shouldn’t do that at all, it’s better to use some ready-made LazyImage, because in the end your approach will have certain weak points - markov
  • 2
    Googled - it looks like your approach with NSDataReadingUncached should work. There is one more approach - to add to urla "? Random =" + random number - markov
  • 2
    @markov Maybe a person wants to figure out how it all works. - Roman Podymov
  • @RomanPodymov I have a link to a picture, a button, into which I upload an image obtained for that link through the function mentioned above in the post. When the user selects an avatar from the device, it is immediately loaded into the button instead of the previous image downloaded from the Internet. And after that, the user presses another button that sends a picture to the server, where this picture replaces the previous one and becomes available for that old link. After that, I call the function described in the post again, where I get the picture from the link and load it into the button. - cheerful_weasel

0