views:

2563

answers:

6

I am trying to capture the contents of a UIWebView including that which is not visible to the user. i.e. The whole web page even though the user is only looking at the top.

Looking around I found the best way to capture a UIView is to retrieve its layer and use renderInContext.

However, UIWebView seems to be using its own CALayer implementation which is behaving a lot more like CATiledLayer, although it is still claiming to be a standard CALayer. When I call renderInContext I only get one portion of the web page, up to 940px down, as opposed to the whole page.

Has anyone got any ideas on how to either: force the UIWebView to scroll down another 940px (obviously that is far from ideal) or tell whatever breed of CALayer it is backing the WebView to render all of its content when I ask it to.

Cheers

EDIT: I should add that currently change the frame of the webview to fit the size of the page retrieved through javascript.

A: 

I'm not sure what you're trying to do, but maybe UIGraphicsBeginImageContext/drawRect/UIGraphicsEndImageContext might work than renderInContext?

See http://stackoverflow.com/questions/449683/saving-a-view-as-a-photo-in-iphone-app for an example.

Note that I have no idea how the UIWebView will react to drawRect.

Kristopher Johnson
I did give that a go, but drawRect results in nothing from the UIWebView :/
Danny Greg
UIWebView does all it's drawing on a subview that it creates. You could try looking through the `subviews` collection and sending draw rect to each
rpetrich
+1  A: 

It looks to me like UIWebView renders on demand (witness the checkerboard as you scroll downwards rapidly on a large page), so there won't be anything in the part of the layer below what you can reach until the user scrolls down there. The layer won't be able to write out what it doesn't have, so I don't think you'll be able to capture the whole area.

As far as scrolling, there aren't any obvious exposed API methods that I can think of to move it down. The web view seems to host a UIScrollView or something similar, which you could try to access by traversing the view hierarchy and use its scrollRectToVisible:animated:, but that doesn't sound like a good long-term solution.

Brad Larson
Yeah the webview acts as if it has a CATiledLayer, and only renders chunks at a time, if it was a true CATiledLayer it wouldnt be so bad as I could set the tile size, but unfortunately this isnt the case.I am playing around with trying to scroll the layer but it isnt liking that either.
Danny Greg
+1  A: 

Just to answer one of my questions, you can scroll the view by using javascript.

So you use stringByEvaluatingJavaScriptFromString: with a javascript of window.scrollTo(0,%i);, where %i is where you want to scroll to. You can also use scrollY to retrieve the current scroll value.

Danny Greg
A: 

All UIViews have a size limit of 1024x1024, so you will probably have programmatically scroll, capture the page in chunks, and stitch them together somehow.

benzado
A: 

Hi- did you ever figure out how to get the full page rendered from UIWebView?

mary
A: 

Hello, Did you figure out how to do that ? Thanks

xicoras