【发布时间】:2012-07-17 05:02:51
【问题描述】:
我的应用程序包中有一些嵌入的 html。我正在使用 loadHtmlString 来加载它。同时我的文档文件夹中有一些图像文件。我想用我的文档文件夹中的那个替换我在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];
(文本是我的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];
【问题讨论】: