1
votes

I have web page having 1000's of images and values. Whenever I load the page to UIWebView it gets loaded with values but as there are so many images it takes time to download.

So is there any way, I can download the page from web but images from local.

Any help is really appreciated.

Thank you,

Ankita

3
can you please give me some more details..ajay
Let me know what details you need? In simple example I can say I have page google.com and i want it's image to be loaded from local and all other web page to be loaded from URL google.com. Make sense?Anks

3 Answers

4
votes

If you have your HTML from webserver and images at local

  1. yourBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];

  2. [webView loadHTMLString:@"YourHTML" baseURL:yourBaseURL]

and your HTML contains name of local image stored like

Image Tag like --> imgage src='fig1.jpg width=150 height=150 align="left"

Than this should work.

Not exactly as you required though this way you can load local images in webView.

1
votes

Basically:

Download HTML from server.

NSURL *URL = [[NSURL alloc] initWithString:@"http://www.example.com/page.php"];
NSError *error = nil;
NSStringEncoding encoding;
NSString *theSource = [[NSString alloc] initWithContentsOfURL:URL usedEncoding:&encoding error:&error];

Replace the file references to load images locally.
Note the double slashes, this seems important.

// Replace:
<img src="File.png">
// By something like:
<img src="file://Path//To//Resources//File.png">

Detailed information how to do this (check post by Joe D'Andrea):
Link to resources inside WebView - iPhone

Finally make the UIWebView load the HTML:

[webView loadHTMLString:htmlString baseURL:baseURL];

Now it should load the 'fresh' HTML from the server with local images.

0
votes

This works quite well for me.

NSURL *myBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *myString = [[managedObject valueForKey:@"long_presentation"] stringByReplacingOccurrencesOfString:@"/FooFolder/BarFolder" withString:[NSString stringWithFormat:@"%@/FooFolder/BarFolder", [[NSBundle mainBundle] bundlePath] ] ];
[attractionWebView loadHTMLString:myString baseURL:myBaseURL];

I had to include a replacement for the image source

img src="FooFolder/BarFolder/my_image.jpg" ...

to get the right path.