我的应用程序包中有一些嵌入式html.我正在使用loadHtmlString来加载它.同时我的文档文件夹中有一些图像文件.我想用html中的img替换我的文档文件夹中的文件.例如,这是我有的示例html:
<div id="image">
<a href="image">
<img src="@image" class="center">
</a>
</div>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码将@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];
Run Code Online (Sandbox Code Playgroud)
(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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2322 次 |
| 最近记录: |