How to load images with HTML file in UIWebView

空扰寡人 提交于 2019-12-11 01:36:11

问题


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?


回答1:


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:




回答2:


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



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/11169763/how-to-load-images-with-html-file-in-uiwebview

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