5

I have an iPhone app with an embedded UIWebview (Safari) control. I'd like to be able to store the images from certain webpages locally.

Can I programmatically access the path where UIWebview downloads images? Or do I need to parse the HTML and then go find the image resource out on the net and download it again?

3 Answers3

5

I do not know if you can access the preloaded images from Safari's cache...

However you can easily find the image URLs without parsing HTML, by running a bit of javascript instead:

NSString *script = @"var n = document.images.length; var names = [];"
                    "for (var i = 0; i < n; i++) {"
                    "     names.push(document.images[i].src);"
                    "} String(names);";
NSString *urls = [webView stringByEvaluatingJavaScriptFromString:script];
// urls contains a comma-separated list of the image URLs.
squelart
  • 11,261
  • 3
  • 39
  • 43
  • 1
    And you just need to add NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.url.com/image.jpeg"]]; UIImage *theImage = [UIImage imageWithData:imageData]; UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil); to save the image to Photos album. – Chintan Patel Jul 08 '11 at 20:21
  • This is not a solution the author asking for. He doesn't want to request for images on the internet as they are loaded in webview already. I've voted up the question. – kas-kad Feb 05 '14 at 09:06
0

I don't think you can save the images using the UIWebview directly, you'r going to have to fetch the images manually...

hhafez
  • 38,949
  • 39
  • 113
  • 143
0

disclaimer: I have not tried this.

If you can find out their names, UIImage responds to +(UIImage*)imageNamed:(NSString*) which the docs imply might get the image back from a device wide cache.

I'd be interested in knowing the results of any experiments.

Rog
  • 17,070
  • 9
  • 50
  • 73