UIWebView的baseURL设置为NSDocumentDirectory?

时间:2012-06-06 15:01:25

标签: iphone ios

是否可以将UIWebView的baseURL设置为NSDocumentDirectory?如果是,那怎么做呢?

我要做的是在/ img src =“”/ tag中本地显示一些下载的图像。

提前感谢任何建议。

2 个答案:

答案 0 :(得分:0)

要在webview中加载本地文件,请执行以下操作

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"
<img src="YOUR_IMAGE.jpg" alt="" width="100" height="100" />";

[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

这应该在UIWebView中加载YOUR_IMAGE.jpg 从中了解更多 http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/

答案 1 :(得分:0)

你可以尝试做这个人所做的事:UIWebView: Absolute path for images in app documents folder

基本上,它使用文档目录路径作为图像源,而不仅仅是img src =“image_name”