• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Capture WebView in a NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Capture WebView in a NSImage


  • Subject: Capture WebView in a NSImage
  • From: "Marcus S. Zarra" <email@hidden>
  • Date: Wed, 26 Jul 2006 12:26:41 -0600

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@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Window levels - is this safe?
  • Next by Date: (no subject)
  • Previous by thread: Re: NSString** in scanners
  • Next by thread: NSTextView, style, and weird Core Data issue
  • Index(es):
    • Date
    • Thread