使用本地映像文件时清除UIWebView缓存

时间:2023-01-16 20:23:44

I use a UIWebView to load a local html, and there is a PNG file inside the html created by Objc.

我使用UIWebView加载本地html,并且在Objc创建的html中有一个PNG文件。

After the PNG file has been modified, I reload the html in UIWebView, but the image doesn't change. However, if I quit the app and reopen it, the image file will be changed to the new one.

修改PNG文件后,我在UIWebView中重新加载html,但图像不会改变。但是,如果我退出应用程序并重新打开它,图像文件将更改为新文件。

I have checked the PNG file in Documents with Finder, so I'm sure it has been modified, but the old one is still in UIWebView.

我已经在Documents with Finder中检查了PNG文件,所以我确定它已被修改,但旧的仍在UIWebView中。

So, as I think that it's a UIWebView cache problem, I've tried:

所以,我认为这是一个UIWebView缓存问题,我试过:

  • [[NSURLCache sharedURLCache] removeAllCachedResponses];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

  • [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:url isDirectory:NO ] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:1]]; or NSURLRequestReloadIgnoringCacheData

    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:url isDirectory:NO] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:1]];或NSURLRequestReloadIgnoringCacheData

None of them works, and I can't change the filename, because the PNG file is used in a lot of places (other html and objc code).

它们都不起作用,我无法更改文件名,因为PNG文件在很多地方使用(其他html和objc代码)。

I've tried this too: some.png?r=randomNumber but it can't be showed.

我也试过这个:some.png?r = randomNumber但是无法显示。

How do I clear the UIWebView cache when using a local image file inside a local html?

在本地html中使用本地图像文件时,如何清除UIWebView缓存?

3 个解决方案

#1


2  

You can try this, in your AppDelegate.m

您可以在AppDelegate.m中尝试此操作

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}

#2


4  

Other than renaming every file on each access, I've only seen one thing work for this and that is modifying the HTML with javascript to add a timestamp onto the image url so it tricks the webview into thinking it's a different file. Images (usually) load the same no matter what you put after the ? in their url. I think this would be easier than renaming every file each time you load the web view. Something like this (using jQuery):

除了在每次访问时重命名每个文件之外,我只看到一件事情可以解决这个问题,即使用javascript修改HTML以在图像网址上添加时间戳,这样就可以让webview认为它是一个不同的文件。图像(通常)加载相同,无论你放在什么后?在他们的网址中。我认为这比每次加载Web视图时重命名每个文件更容易。像这样(使用jQuery):

<img src="myimg.jpg" />

<script>
$(function() {
    $('img').each(function(i, el) {
        $(el).attr('src', $(el).attr('src')+'?pizza='+(new Date()).getTime());
    });
});
</script>

I guess this is assuming that this javascript loads and runs before the images are loaded, but in theory this should work.

我想这是假设这个javascript在加载图像之前加载并运行,但理论上这应该有效。

For a little background, I've made a page in a webview that used RequireJS to asynchronously load quite a few files. I had the EXACT same problem that this question is talking about except that I was loading javascript files instead of images. The key to fixing this issue was adding a timestamp to every path of javascript file and thus tricking the webview (ex me.js?ts=236136236 and whatever.js?ts=3153524623). I found this to work great.

对于一些背景知识,我在webview中创建了一个页面,该页面使用RequireJS来异步加载相当多的文件。除了我正在加载javascript文件而不是图像之外,我遇到了与此问题相同的问题。解决这个问题的关键是为javascript文件的每个路径添加一个时间戳,从而欺骗webview(ex me.js?ts = 236136236和whatever.js?ts = 3153524623)。我觉得这很好用。

One other thing I needed to do was add a timestamp to the path of my HTML file when loading it into the webview like this:

我需要做的另一件事是在将HTML文件加载到webview中时为我的HTML文件路径添加时间戳,如下所示:

[NSURL URLWithString:[[NSString stringWithFormat:@"%@/index.html?pizza=%f", webDirectoryPath, [[NSDate date] timeIntervalSince1970]]

I now can modify all the local files and each time the webview appears the changes come through.

我现在可以修改所有本地文件,每次webview出现时都会发生变化。

#3


1  

If your html didn't change and the only change was in image you should use UIWebView's reload message instead of loading request again.

如果您的html没有更改,并且唯一的更改是在图像中,您应该使用UIWebView的重新加载消息,而不是再次加载请求。

Something like this:

像这样的东西:

- (void)reloadWebView:(id)sender
{
    if (self.loaded) {
        [self.webView reload];
    } else {
        NSString *html = [[NSBundle mainBundle] pathForResource:@"web" ofType:@"html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html]];
        [self.webView loadRequest:request];
        self.loaded = YES;
    }
}

You don't even need any manipulations with cache.

你甚至不需要对缓存进行任何操作。

#1


2  

You can try this, in your AppDelegate.m

您可以在AppDelegate.m中尝试此操作

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}

#2


4  

Other than renaming every file on each access, I've only seen one thing work for this and that is modifying the HTML with javascript to add a timestamp onto the image url so it tricks the webview into thinking it's a different file. Images (usually) load the same no matter what you put after the ? in their url. I think this would be easier than renaming every file each time you load the web view. Something like this (using jQuery):

除了在每次访问时重命名每个文件之外,我只看到一件事情可以解决这个问题,即使用javascript修改HTML以在图像网址上添加时间戳,这样就可以让webview认为它是一个不同的文件。图像(通常)加载相同,无论你放在什么后?在他们的网址中。我认为这比每次加载Web视图时重命名每个文件更容易。像这样(使用jQuery):

<img src="myimg.jpg" />

<script>
$(function() {
    $('img').each(function(i, el) {
        $(el).attr('src', $(el).attr('src')+'?pizza='+(new Date()).getTime());
    });
});
</script>

I guess this is assuming that this javascript loads and runs before the images are loaded, but in theory this should work.

我想这是假设这个javascript在加载图像之前加载并运行,但理论上这应该有效。

For a little background, I've made a page in a webview that used RequireJS to asynchronously load quite a few files. I had the EXACT same problem that this question is talking about except that I was loading javascript files instead of images. The key to fixing this issue was adding a timestamp to every path of javascript file and thus tricking the webview (ex me.js?ts=236136236 and whatever.js?ts=3153524623). I found this to work great.

对于一些背景知识,我在webview中创建了一个页面,该页面使用RequireJS来异步加载相当多的文件。除了我正在加载javascript文件而不是图像之外,我遇到了与此问题相同的问题。解决这个问题的关键是为javascript文件的每个路径添加一个时间戳,从而欺骗webview(ex me.js?ts = 236136236和whatever.js?ts = 3153524623)。我觉得这很好用。

One other thing I needed to do was add a timestamp to the path of my HTML file when loading it into the webview like this:

我需要做的另一件事是在将HTML文件加载到webview中时为我的HTML文件路径添加时间戳,如下所示:

[NSURL URLWithString:[[NSString stringWithFormat:@"%@/index.html?pizza=%f", webDirectoryPath, [[NSDate date] timeIntervalSince1970]]

I now can modify all the local files and each time the webview appears the changes come through.

我现在可以修改所有本地文件,每次webview出现时都会发生变化。

#3


1  

If your html didn't change and the only change was in image you should use UIWebView's reload message instead of loading request again.

如果您的html没有更改,并且唯一的更改是在图像中,您应该使用UIWebView的重新加载消息,而不是再次加载请求。

Something like this:

像这样的东西:

- (void)reloadWebView:(id)sender
{
    if (self.loaded) {
        [self.webView reload];
    } else {
        NSString *html = [[NSBundle mainBundle] pathForResource:@"web" ofType:@"html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html]];
        [self.webView loadRequest:request];
        self.loaded = YES;
    }
}

You don't even need any manipulations with cache.

你甚至不需要对缓存进行任何操作。