0

I have a UIWebView which has images in it, say that I have the point/coordinates of the x y position of the image, width and height. Can I create an image just by specifying the location of the image in UIWebView? If yes how?

Or is there a way to get an UIImage from a UIWebView without actually looking at the <img src=""> of the html tags? Basically I don't want to make a connection to the internet to pull an image as the image is already there seen on the screen. Question is how do I extract it?

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
adit
  • 32,574
  • 72
  • 229
  • 373

2 Answers2

0

Depending on the cache policy in place, a request to retrieve the image may not result in another connection to download the image... But to answer your actual question...

You can take a 'snapshot' of the UIWebView and then crop to the location of the image in question. Check this question out for details on how to do this: How Do I Take a Screen Shot of a UIView?

Community
  • 1
  • 1
ikuramedia
  • 6,038
  • 3
  • 28
  • 31
  • I am interested about the cache policy in place. What do you mean by that? – adit Jan 19 '12 at 19:34
  • If you load an NSURLRequest object, you can control the cache policy: http://developer.apple.com/library/ios/ipad/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html - if you read the section on cache policy you can choose the correct one for your needs. Note though that if the originating server says 'don't cache' then UIWebView will still have to redownload the image. Typically this won't be a problem for static content. – ikuramedia Jan 19 '12 at 19:40
  • wait so basically if my UIWebView has an image () in it which pulls it from an URL. I can somehow get the NSData given a specific URL? – adit Jan 19 '12 at 19:43
  • Yes, NSURLConnection will load an NSData with the content from the destination URL. If that content has been locally cached, then it should pull the data from the cache, rather than initiating a new connection. If you specify `NSURLRequestReturnCacheDataDontLoad` in your NSURLRequest, then you can test whether this will work for you or not (the load will fail if the content is not available from the cache) – ikuramedia Jan 20 '12 at 06:51
0

This link explain a way to get the image just by grabbing it from the specified coordinates. How to create a UIImage from the current graphics context?.

However there might be a more elegant solution of using some javascript to get the original image by using the stringByEvaluatingJavaScriptFromString: method of UIWebview. But I am not sure if javascript is capable of doing it

Community
  • 1
  • 1
aqs
  • 5,632
  • 3
  • 24
  • 24