在本地HTML文件中加载图像 - UIWebView

时间:2021-06-23 19:53:41

I load a local HTML file in a UIWebView like so:

我在UIWebView中加载一个本地HTML文件,如下所示:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];    

In my index.html, how do I load a display a local image (added to project through Xcode) with img src="..."/> ?

在我的index.html中,如何使用img src =“...”/>加载显示本地图像(通过Xcode添加到项目中)?

1 个解决方案

#1


7  

Instead of loading with an NSURLRequest, read the html file into an NSString and create an NSURL to the directory with image files.

而不是使用NSURLRequest加载,将html文件读入NSString并使用图像文件创建NSURL到目录。

Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method.

然后使用UIWebView的 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL方法。

So, you'd have something like...

那么,你会有......

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];

#1


7  

Instead of loading with an NSURLRequest, read the html file into an NSString and create an NSURL to the directory with image files.

而不是使用NSURLRequest加载,将html文件读入NSString并使用图像文件创建NSURL到目录。

Then use UIWebView's - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL method.

然后使用UIWebView的 - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL方法。

So, you'd have something like...

那么,你会有......

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[webView loadHTMLString:html baseURL:[url URLByDeletingLastPathComponent]];