Re: Drawing a view and its subviews to an NSImage
Re: Drawing a view and its subviews to an NSImage
- Subject: Re: Drawing a view and its subviews to an NSImage
- From: Shawn Erickson <email@hidden>
- Date: Sat, 5 Jun 2004 08:25:08 -0700
On Jun 5, 2004, at 2:30 AM, Mark A. Stratman wrote:
On Jun 5, 2004, at 1:08 AM, Mark Alldritt wrote:
Hi Folks,
I'm looking for some clues on how to draw a view and all of its
subviews to
an NSImage.
In searching the archives I found a reference to something called
BTOffscreenView, but the download link is no longer valid.
If I'm understanding correctly, you'll probably want to use NSView's
dataWithEPSInsideRect:
Example:
NSData *eps = [aView dataWithEPSInsideRect:[aView bounds]];
NSImage *image = [[NSImage alloc] initWithData:eps];
This produces an image that looks exactly like what you see on the
screen.
I believe dataWithPDFInsideRect: is the more modern one of those.
It depends on what you are trying to do but also consider using
NSBitmapImageRep's initWithFocusedViewRect: method.
<
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSBitmapImageRep.html#//apple_ref/
doc/uid/20000347/initWithFocusedViewRectCOLON>
So something like...
[theTopLevelView lockFocus];
[bitmap initWithFocusedViewRect:[theTopLevelView bounds]]
[theTopLevelView unlockFocus];
For example: <
http://developer.apple.com/qa/qa2001/qa1325.html>
You should also be able to create an image of the appropriate size and
lockFocus on it then message drawRect: of the top level view yourself
(never tried it this way but it should work).
What I often have needed to do is draw parts of custom view into a
NSImage to avoid having to redraw complex paths, etc. to often because
they are to expensive. Instead simple draw them once and redraw them
when they change but in between simply composite the static image. So
something like...
...update image method...
[cachedImage lockFocus];
//draw complex paths, etc.
[cachedImage unlockFocus];
...and later in the views draw rect...
[cachedImage compositeToPoint:<some point> operation:<some operation>
(Use NSCompositeCopy operation if you image is opaque since it is
faster otherwise likely use NSCompositeSourceOver)
Finally review how printing is done since that can give you PDFs, etc.
of your views.
<
http://developer.apple.com/documentation/Cocoa/Conceptual/Printing/
index.html#//apple_ref/doc/uid/10000083i> and in particular
<
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSPrintOperation.html#//apple_ref/
doc/uid/20000362>.
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.