在本地HTML文件中加载图像 - UIWebView [英] Load image in local HTML file -- UIWebView

查看:115
本文介绍了在本地HTML文件中加载图像 - UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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

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

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

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

这篇关于在本地HTML文件中加载图像 - UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆