Eager Beaver
Eager Beaver

Reputation: 989

How to load images with HTML file in UIWebView

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?

Upvotes: 2

Views: 5665

Answers (4)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

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

Upvotes: 2

The Windwaker
The Windwaker

Reputation: 1054

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

Upvotes: 0

iDhaval
iDhaval

Reputation: 7844

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

Upvotes: 1

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

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.

Upvotes: 0

Related Questions