【问题标题】:Rending html in UIWebView and load images from cache在 UIWebView 中渲染 html 并从缓存中加载图像
【发布时间】:2012-05-21 07:27:36
【问题描述】:

当用户第一次运行我的应用程序时,我已加载所有图像并将其保存到库目录中的缓存文件夹中:Library/Caches/imgcache/myApp/ready/

让我们说文件名: http/__wscdn.bbc.co.uk_worldservice_assets_images_2012_01_13_120113094952_international_space_station_304x171_reuters_nocredit.jpg

现在当用户离开网络时,我正在使用 loadHTMLString 渲染 html,如果 html 包含以前存储文件的任何 img src,它会从缓存而不是从互联网加载,因为用户无法访问互联网。

现在我的问题是我该如何处理这个问题?我怎样才能做到这一点 ?请帮帮我..

例如:

我有一个带有 html 的字符串,

>"<div class="g-container story-body"> <div class="bodytext"> <div class="module "> <div >class="image img-w304"><img width="304" height="171" >src="http://wscdn.bbc.co.uk/worldservice/assets/images/2012/05/18/120518135959_facebook_304>x171_bbc_nocredit.jpg" alt=""/> <div class="module "> <div class="image img-w304"><img >width="304" height="171" >src="http://wscdn.bbc.co.uk/worldservice/assets/images/2012/05/18/120518145635_facebook_304>x171_bbc_nocredit.jpg" alt=""/><p class="caption"></p> </div> </div> <p></p> </div> </div> ><div class="g-container story-body"></div>"

我已将这两张图片缓存在用户文件夹中, 现在,当用户无法访问网络时,我想做这样的事情:

><div class="g-container story-body"> <div class="bodytext"> <div class="module "> <div >class="image img-w304"><img width="304" height="171" >src="**Library/Caches/imgcache/myApp/ready/**http://wscdn.bbc.co.uk/worldservice/assets/ima>ges/2012/05/18/120518135959_facebook_304x171_bbc_nocredit.jpg" alt=""/> <div class="module >"> <div class="image img-w304"><img width="304" height="171" >src="**Library/Caches/imgcache/myApp/ready/**http://wscdn.bbc.co.uk/worldservice/assets/ima>ges/2012/05/18/120518145635_facebook_304x171_bbc_nocredit.jpg" alt=""/><p class="caption">>>``</p> </div> </div> <p></p> </div> </div> <div class="g-container story-body"></div>

【问题讨论】:

  • 你是在描述你已经实现的还是你正在尝试实现的?你到底卡在哪里了?
  • 我已经实现了完整的应用程序,但我想要的是在 UIwebview 中加载 html 时,我想从缓存文件夹而不是从网络加载图像..

标签: iphone ios uiwebview


【解决方案1】:

您可以通过 webview 的- (void)loadHTMLString:(NSString *)stringbaseURL:(NSURL *)baseURL 方法使用图像缓存。您需要将 baseURL 指定到本地应用程序库目录。示例代码如下:

NSString * htmlContent = @"your html content";
NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
[_webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:path]];

如果您的图片位置为“YourApp/Library/imageCache/1.png”,则图片网址应为“./imageCache/1.png”等相关网址。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多