Search code examples
iosobjective-ciphoneuiwebview

load locally stored image in UIWebView


My requirement is to load locally stored image in the UIWebView.

My code is as follows:

NSBundle *mainBundle = [NSBundle mainBundle];
    NSURL *homeIndexUrl = [mainBundle URLForResource:@"web" withExtension:@"html"];
    NSURLRequest *urlReq = [NSURLRequest requestWithURL:homeIndexUrl];
    [self.webView loadRequest:urlReq];

My html file is as follows:

<html>
  <body>
    <img src=“Initial.png”>
  </body>
</html> 

I have referred following links but I am not getting my ultimate output

link1 link2


Solution

  • This thing worked for me:

    The image should not be in the xcassettes.

      NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
     NSString *imgHTMLTag = [NSString stringWithFormat:@"<img src=\"file://%@\" />", imagePath];
     NSURL *Url = [mainBundle URLForResource:@"img_icon" withExtension:@"png"];
    [self.webView loadHTMLString:imgHTMLTag baseURL:Url];