drix
drix

Reputation: 21

UIWebView loading a local image that gets overwritten but it doesn't refresh

My problem is this

I'm loading an html string into an UIWebView, in which i'm referencing a local image from my application's Documents folder.

<html><body><img src="image.png"/></body></html>

It loads just fine, no problems with the baseUrl or anything. The problem is when I do some stuff and generate a new image, with the same name, and save it in the same Documents folder, overwriting the old one (I delete the old one if it exists and save the new one).

Now, with the UIWebView still loaded on the screen, if I do a [webview reload] or manually load the html string again, I still get the old image, the one I have just overwritten.

I already checked in the simulator folder, the image.png there is indeed the new image, not the old one. And I already did everything imaginable to stop the webview from caching.

Of course, if I save the new image with a different name, say "image2.png", and reload the html string with src="image2.png", it all shows up ok.

What am I missing here?

Upvotes: 1

Views: 1099

Answers (2)

major-mann
major-mann

Reputation: 2652

Try appending a random query string to your image source. ie.

function ImageSource(src){
  return src + "?time=" + (new Date()).getTime().toString();
}
function ForceImageReloads(){
  var images = document.getElementsByTagName("img");
  for (var i=0;i<images.length;i++){
    var img = images[i];
    var s = img.getAttribute("src");
    img.src = ImageSource(s);
  }
}
ForceImageReloads();

Or something similar to that :)

Upvotes: 3

Steven
Steven

Reputation: 964

This maybe a problem with the simulator. I had something similar once. Try running your code on a real device. For me, the simulator seem like it is lazy to reload data that have the same name.

Upvotes: 0

Related Questions