2012-06-23 49 views
2

我想使用的代码下面几行加载一个HTML文件中的一个UIWebView中:如何加载的HTML文件中的图像在一个UIWebView

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html"]; 
NSURL *url=[NSURL fileURLWithPath:htmlFile]; 
NSURLRequest *request=[NSURLRequest requestWithURL:url]; 
[WebView loadRequest:request]; 

我能够加载HTML文件,但这个HTML文件还包含一些图像,并且该图像在视图中不加载/可见,任何人都可以为此提出任何解决方案?

+0

[在UIWebView中使用HTML和本地图像](http://stackoverflow.com/questions/747407/using-html-and-local-images-within-uiwebview) – MusiGenesis

+0

http:// stackoverflow。 com/questions/747407/using-html-and -local-images-within-uiwebview –

回答

2

HTML文件:

<HTML> 
<HEAD> 
<TITLE>My Web Page</TITLE> 
</HEAD> 
<BODY> 
<img alt="" src="1.jpg" style="width: 100px; height: 100px;"/> 
<P>This is where you will enter all the text and images you want displayed in a browser window.</P> 
</BODY> 
</HTML> 

web视图代码:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"desk" ofType:@"html"]; 
NSURL *url=[NSURL fileURLWithPath:htmlFile]; 
NSURLRequest *request=[NSURLRequest requestWithURL:url]; 
[_webView loadRequest:request]; 

截图:

enter image description here

+0

如果服务器上的HTML,如何读取本地图片? –

1

您可以使用下面的方式来加载HTML文件,

这样所有的内容被转换成数据,并会在web视图加载。 (所有与用户界面相关的内容,如图片图标应该在项目的资源目录中)

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FileName" ofType:@"html"]; 
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath]; 

if (htmlData) 
    { 
     [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil]; 
    } 
+0

Right Dhawal :) –

0

我假设图像也在包中。您必须确保标签上设置的路径是对包中图像文件位置的引用,或者存储位置的引用。

0

也许你应该尝试把所有你本地网页文件夹中,并使用

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html" inDirectory:@"Your directory"];