• 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
Re: Creating an Offscreen NSView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating an Offscreen NSView


  • Subject: Re: Creating an Offscreen NSView
  • From: Mark Morrill <email@hidden>
  • Date: Mon, 23 May 2005 10:03:23 -0600

Thank you, Thank you!  :)

Mark


On 22-May-05, at 11:29, Ali Lalani wrote:

Oops, i forgot one step, don't forget to [myView unlockFocus] after you create your image rep! 

On 22-May-05, at 1:20 PM, Ali Lalani wrote:

Is creating an offscreen NSView as easy as:
 
  NSView* myOffscreenView = [[NSView alloc] initWithFrame:theFrame];
And is drawing into it as easy as:
 
  [myOffscreenView drawRect:myDrawRect];
Unfortunately not :(

There are a few more steps that are required.
In order to get a view to draw like that it needs to be in a window, so you create a window and put the window offscreen:


  NSWindow *hiddenWindow = [[NSWindow alloc] initWithContentRect:
  NSMakeRect( -1000,-1000,width,height ) // some arbitrary offscreen coordinate
  styleMask: NSTitledWindowMask | NSClosableWindowMask // doesn't matter what these are really
  backing:NSBackingStoreNonretained
  defer:NO]; // if you pass YES for this it won't do any drawing until the window is onscreen, not good for this situation!


Now that you have the window offscreen, with a width and height of the view you want to draw, get the content view of the window and add your view to its subviews:

  [[hiddenWindow contentView] addSubview:myView];

now lock focus on your view

  [myView lockFocus];

call a method on NSBitmapImageRep to get an image from the focused view:

  NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]] autorelease];

make an NSImage from the rep

  NSImage *image = [[[NSImage alloc] init] autorelease];
  [image addRepresentation:rep];

And then clean up your mess by closing the window, etc...

That *should* work, though i've not tested it extensively(i have a category method on NSView that does this), but at least it's a start.


Or getting PDF data from it as easy as:

  NSData* myPDF = [myOffscreenView drawRect:myDrawRect];

This seems to be too easy. Is there a catch? How does the view know not to draw on the screen? How does it allocate a graphics context?

Mark
 

_______________________________________________
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


 _______________________________________________ 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

_______________________________________________ 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
References: 
 >Creating an Offscreen NSView (From: Mark Morrill <email@hidden>)
 >Re: Creating an Offscreen NSView (From: Ali Lalani <email@hidden>)
 >Re: Creating an Offscreen NSView (From: Ali Lalani <email@hidden>)

  • Prev by Date: Re: Printing my document
  • Next by Date: Re: Framework Nibs Not Loading 10.4 to 10.3
  • Previous by thread: Re: Creating an Offscreen NSView
  • Next by thread: Core Data, Document based app, and a structure
  • Index(es):
    • Date
    • Thread