How to load images with HTML file in UIWebView

2019-07-14 04:50发布

I want to load an HTML file in an UIWebView using following lines of code:

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

I am able to load the HTML file, but this HTML file also contain some images, and that images are not loading/visible in the view, can anyone suggest any solution for this?

4条回答
Animai°情兽
2楼-- · 2019-07-14 05:03

HTML File:

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

webView code:

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

screenshot:

enter image description here

查看更多
家丑人穷心不美
3楼-- · 2019-07-14 05:04

Maybe you should try to put all you local webpage in a folder and use

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"01Numbers" ofType:@"html" inDirectory:@"Your directory"];
查看更多
走好不送
4楼-- · 2019-07-14 05:05

You can use following way to load HTML file,

In this way all content get converted into Data and will load in webView. (All UI related contents like images icons should be in Resources Dir of project)

  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];  
  }  
查看更多
一纸荒年 Trace。
5楼-- · 2019-07-14 05:21

I assume the images are also in the bundle. You will have to make sure the path set on the tag is a reference to the location of the image file IN the bundle or wherever you are storing that.

查看更多
登录 后发表回答