如何清除我的UIWebView的loadHtmlString的缓存?



我的应用程序包中嵌入了一些html。我正在使用loadHtmlString来加载它。同时,我的文档文件夹中有一些图像文件。我想用文档文件夹中的img替换html中的img。例如,下面是我的示例html:

       <div id="image">
            <a href="image">
                <img src="@image" class="center">               
            </a>
        </div>

我使用以下代码将@image替换为我文档中的文件路径:

    NSString* imgFile = [NSString stringWithFormat:@"file:///%@/%@",
                         [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images"],@"myimage.png"];
    text = [text stringByReplacingOccurrencesOfString:@"@image" withString:imgFile];   
    NSURL* baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
    [self.webView loadHTMLString:text baseURL:baseUrl];    

(text是我的html文件的html文本)。

它在第一次调用此代码时工作。但例如,如果我修改myimage.png并再次调用代码,那么修改前的图像仍然显示。(我已经在我的文档文件夹中检查了我的文件,它已经被正确修改了)。

我怀疑是缓存造成的。有人能提出建议吗?

感谢

注意:

它是由缓存引起的。我通过使用以下方法强制UIWebView加载图像的新副本来解决问题:

NSString* imgFile = [NSString stringWithFormat:@"file:///%@/%@?t=%d",
                         [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Images"],@"myimage.png",time(NULL)];
    text = [text stringByReplacingOccurrencesOfString:@"@image" withString:imgFile];

我通过在url中添加时间解决了这个问题。因此,图像将不会被缓存。请参考我的问题。这个问题被认为已经结束了。

最新更新