1

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

Anks
  • 225
  • 4
  • 15
  • can you please give me some more details.. – ajay Apr 13 '11 at 10:21
  • 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 http://www.google.com. Make sense? – Anks Apr 13 '11 at 10:33

3 Answers3

4

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.

Community
  • 1
  • 1
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • I didn't get this exactly. Whenever i put something into loadHTMLString does it replace something coming from base URL? Can you please explain? – Anks Apr 13 '11 at 10:59
1

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.

Community
  • 1
  • 1
Anne
  • 26,765
  • 9
  • 65
  • 71
  • Don't you think it's going to take same time to give source or load the page in webview? Becuase we are anyways pinging server to get the HTML file and we will again ping to load webview. – Anks Apr 13 '11 at 10:42
  • I just answered exactly what you where asking for. UIWebView normally: Download source => Render => Download additional files => Display. In this case: Download source => Change source => Render => Load local files => Display. The 'source edit' step does not make any notable difference, it's just a few string replacements. So (practically) no influence on the performance and you save the download time. **Note:** You don't 'ping' the UIWebView, you only pass the source manually. **Note**: Better get the data in XML format to the iPhone instead of scraping the website, or is it not your server? – Anne Apr 14 '11 at 10:47
0

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.

DevonDahon
  • 7,460
  • 6
  • 69
  • 114