1

I try to load Local HTML page in my web view. It loads but Images are not loaded.

my Structure of file is as follow:

enter image description here

Image Path in index.html

<div class = "logo mt10 text-center"> <img src="images/logo.png" alt="Logo Goes Here"> </div>

Load HTML Page in Web view

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    [web loadHTMLString:htmlString baseURL:baseURL];

Help me to solve this

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2893370
  • 701
  • 1
  • 9
  • 23

5 Answers5

1

Hey you can use following way of doing this :

By Using relative paths to your file or folder your local html with relative references will work for you.

Follow the steps :

  1. Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders".
  2. Select the "create folder references" option.
  3. Use the following code to load the files using relative path.

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
    

    [webview loadRequest:[NSURLRequest requestWithURL:url]];

Your all your relative links(like img/.gif, js/.js) will gets resolve.

You can then refer to your images like this:

<img src="myimage.png">

i think your problem is only because of the relative path . Please follow the mentioned steps. You will see the image loaded on webview.

Shankar Aware
  • 128
  • 1
  • 11
0

You have to give full path of the image in the bundle along with adding file:// as a prefix as below Change your image path to ##image## as below

<div class = "logo mt10 text-center"> <img src="##image##" alt="Logo Goes Here"> </div>

Then do this change in your code

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
htmlString =[htmlString stringByReplacingOccurrencesOfString:@"##image##" withString:[NSString stringWithFormat:@"file://%@",[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]]];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[web loadHTMLString:htmlString baseURL:baseURL];
if-else-switch
  • 977
  • 1
  • 7
  • 24
  • That is not what i am saying. I said you to change the image path respective to bundle. Wait i am editing my answer with code – if-else-switch Jun 19 '14 at 12:12
0

The path you have given for image is: src="images/logo.png"

And the path is not found by XCode.

If you want your path to be like this, you will have to check the create folder references for any added folders option in xcode, while adding the entire html folder and the image folder too.

After this you will get the folder images as blue colored folder instead of yellow that you get now.

Otherwise change your path and remove images/. Just keep logo.png in src.

Hope that helps.

z22
  • 10,013
  • 17
  • 70
  • 126
0

You need to add your HTML folder as reference.

Then load your HTML like:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"HTML"]];
[web loadRequest:[NSURLRequest requestWithURL:url]];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

It is the way you load your HTML file to the webview. The paths should be relative to that.

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

and Load image in the form of

<img src="myimage.png">

Refer this for details

Community
  • 1
  • 1
Ganesh Somani
  • 2,280
  • 2
  • 28
  • 37