Skip navigation.
 
mlCapture WebView in a NSImage
FROM : Marcus S. Zarra
DATE : Wed Jul 26 20:26:41 2006

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

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