Skip navigation.
 
mlRe: Capture WebView in a NSImage
FROM : Dan Bernstein
DATE : Wed Aug 16 10:33:58 2006

I think sending -cacheDisplayInRect:toBitmapImageRep: to the WebView
will do the trick. You can obtain an appropriate image rep using
-bitmapImageRepForCachingDisplayInRect:

On 7/26/06, Marcus S. Zarra <<email_removed>> wrote:
> I am trying to capture a WebView in an NSImage but I am running into
> issues.  Having searched Google, etc. I found a couple of exmples but
> seem to be missing something as it  is not working.
>
> In my app, I init a WebView and give it a NSURLRequest and set a load
> delegate.  In the delegate I have the following code:
>
> - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
> {
>    NSRect workingRect = NSMakeRect(0, 0, 400, 400);
>    NSWindow *tempWindow = [[NSWindow alloc]
> initWithContentRect:workingRect styleMask:NSBorderlessWindowMask
> backing:NSBackingStoreBuffered defer:NO];
>    [tempWindow setContentView:sender];
>
>    NSImage *image = [[NSImage alloc] initWithSize:workingRect.size];
>    [image lockFocus];
>    [sender drawRect:workingRect];
>    [image unlockFocus];
>
>    NSWindow *imageWindow = [[NSWindow alloc]
> initWithContentRect:workingRect styleMask:NSBorderlessWindowMask
> backing:NSBackingStoreBuffered defer:NO];
>    NSImageView *imageView = [[NSImageView alloc]
> initWithFrame:[imageWindow frame]];
>    [imageView setImage:image];
>    [imageWindow setContentView:imageView];
>    [imageWindow orderFront:nil];
> }
>
> If I call -orderFront: on the tempWindow I see the webpage as one
> would expect.  However the image is not populated with the webview.
>
> I have also tried generating the image as such:
>
> - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
> {
>    NSRect workingRect = NSMakeRect(0, 0, 800, 800);
>    NSWindow *tempWindow = [[NSWindow alloc]
> initWithContentRect:workingRect styleMask:NSBorderlessWindowMask
> backing:NSBackingStoreBuffered defer:NO];
>    [[tempWindow contentView] addSubview:sender];
>        [sender lockFocus];
>        NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc]
> initWithFocusedViewRect:workingRect] autorelease];
>        NSImage *image = [[[NSImage alloc] init] autorelease];
>        [image addRepresentation:rep];
>        [sender unlockFocus];
>
>    NSWindow *imageWindow = [[NSWindow alloc]
> initWithContentRect:workingRect styleMask:NSBorderlessWindowMask
> backing:NSBackingStoreBuffered defer:NO];
>    NSImageView *imageView = [[NSImageView alloc]
> initWithFrame:[imageWindow frame]];
>    [imageView setImage:image];
>    [imageWindow setContentView:imageView];
>    [imageWindow orderFront:nil];
> }
>
> With the same result.  What am I missing?
>
> Thanks for any help.
>
> Marcus S. Zarra
> Zarra Studios LLC
> www.zarrastudios.com
> Simply Elegant Software for OS X
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/dan.<email_removed>
>
> This email sent to dan.<email_removed>
>

Related mailsAuthorDate
mlCapture WebView in a NSImage Marcus S. Zarra Jul 26, 20:26
mlRe: Capture WebView in a NSImage Dan Bernstein Aug 16, 10:33
mlRe: Capture WebView in a NSImage cocoa Aug 16, 12:02