Load image in local HTML file — UIWebView

浪尽此生 提交于 2020-01-10 03:27:07

问题


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

[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="..."/> ?


回答1:


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

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

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]];


来源:https://stackoverflow.com/questions/6147747/load-image-in-local-html-file-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!