3

I'm trying to write code that loads a local html string into a UIWebView with images from the Assets.xcassets catalog.

Currently, I load the images from the main bundle, like this:

NSString *htmlString = [NSString stringWithFormat:@"<h1>My title</h1><img src='%@'/>", @"myImage1.png"];
[self.webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];

However, this is very limiting in respect to using different retina versions of the images (@2 and @3). I would like to load the images instead from the asset catalog, but can't figure out how to do it.

Provided the image name in the html string, how do I load the image from the assests into the webView, while supporting @2 and @3 retina versions?

amirfl
  • 1,634
  • 2
  • 18
  • 34

1 Answers1

-1

Here Is Given Code Try it..

NSString *path = [[NSBundle mainBundle] bundlePath];

NSURL *baseURL = [NSURL fileURLWithPath:path];

[webView loadHTMLString:htmlString baseURL:baseURL]; ==> like thi some images :

=================================================================

This is how to load/use a local html with relative references. Use the below given code.

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

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

Now all your relative links(like img/.gif, js/.js) in the html should get resolved.

Akash
  • 461
  • 2
  • 14
  • 1
    This doesn't solve how to use images from the Asset catalog. – amirfl Jan 06 '16 at 15:41
  • check try it :http://stackoverflow.com/questions/17052344/access-asset-catalog-programmatically – Akash Jan 06 '16 at 16:46
  • 1
    thank you for answering, but this doesn't answer the question. I'm not trying to load an image from the asset catalog straight into a UIImage. I'm trying to load images from the asset catalog into html in a UIWebView while resolving retina sizes. – amirfl Jan 06 '16 at 18:36