从UIwebview加载图像失败

时间:2013-07-13 15:10:32

标签: ios uiwebview

我正在使用Webview加载存储在我的应用程序库目录中的图像文件,首先我尝试使用resourcePath和捆绑路径

NSString * html = [[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", filename];
[self.webView loadHTMLString:newhtml  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

问题是无论我在baseUrl中设置什么,我无法正确加载图像,我也尝试过filePath:

[self.webView loadHTMLString:newhtml  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] filePath]]];

但是如果我在html中设置图像文件的绝对路径,那么所有的东西都没问题,我想知道为什么?

1 个答案:

答案 0 :(得分:1)

看一下这篇文章:Using HTML and Local Images Within UIWebView

最佳答案明确表示使用文件:路径引用图像不适用于UIWebView。

您需要做的就是传递基本路径。例如:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

然后只需要像这样引用它:<img src="myimage.png">