Re: Image from WebView is sometimes blank
Re: Image from WebView is sometimes blank
- Subject: Re: Image from WebView is sometimes blank
- From: "Adam R. Maxwell" <email@hidden>
- Date: Fri, 14 May 2010 11:27:57 -0700
On May 11, 2010, at 2:33 PM, Duncan Oliver wrote:
> I have a WebView that I use to capture a thumbnail of a loaded webpage
> using cacheDisplayInRect:. The WebView object is created
> programmatically and not attached to a window. About half the time,
> the image it grabs is blank. I can't seem to figure out if I'm trying
> to grab it too soon or what. Would love to know how to get a completed
> image all the time. Thanks in advance.
This can be kind of tricky, especially when you need to make it work for arbitrary pages; just figuring out when the webview finishes loading is tricky, since -[WebView isLoading] doesn't account for redirection.
>
> Here's the code:
>
> - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
> {
> if ([frame isEqualTo:[sender mainFrame]])
> {
My first guess is that pages aren't fully loaded when you get this callback, but that's generally only a problem with redirects.
> if ([sender saveThumbnail] == YES)
> {
> NSView *mainFrameView = sender.mainFrame.frameView;
> NSBitmapImageRep *thumbnailImageRep = [mainFrameView
> bitmapImageRepForCachingDisplayInRect:[mainFrameView frame]];
> [sender cacheDisplayInRect:[mainFrameView frame]
> toBitmapImageRep:thumbnailImageRep];
Another potential problem is that you need to force layout in the WebDocumentView; this shouldn't be necessary, but I couldn't find any other workaround. Try sending
[[mainFrameView documentView] layout];
before calling -cacheDisplayInRect:toBitmapImageRep:. This fixed a bunch of random white page issues for me. If you use -[WebFrameView setAllowsScrolling:NO] at some point, the problem is much worse (as of Safari 4.0.3).
I have a BSD-licensed web thumbnail class here:
http://code.google.com/p/fileview/source/browse/trunk/fileview/FVWebViewIcon.m
It's complicated by threading issues, but shows one way to track redirected frames during loading, and also how to draw.
hth,
Adam
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden