Re: Rendering an NSControl into a NSImage?
Re: Rendering an NSControl into a NSImage?
- Subject: Re: Rendering an NSControl into a NSImage?
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 1 Apr 2003 14:20:13 -0800
On Tuesday, April 1, 2003, at 01:00 PM, Mark Alldritt wrote:
I'm looking for an example showing how to render a NSControl (e.g.
NSButton)
into an NSImage.
Sample code:
http://developer.apple.com/samplecode/Sample_Code/Cocoa/
Color_Sampler.htm
See the files "Snapshot.[hm]"
Thanks for the pointer. However, the problem with this sample is that
it
(a) requires that the control be on-screen, and (b) includes the window
background. I want to capture the image of the control without the
background.
In that case, try changing:
- (NSImage *) snapshotFromRect:(NSRect) sourceRect;
/*"This method creates a new image from a portion of the receiving
view. The image is returned autoreleased."*/
{
NSImage
*snapshot = [[NSImage alloc] initWithSize:sourceRect.size];
NSBitmapImageRep
*rep;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:sourceRect];
[self unlockFocus];
[snapshot addRepresentation:rep];
return [snapshot autorelease]; // balance the +alloc call..
}
to
- (NSImage *) snapshotFromRect:(NSRect) sourceRect;
/*"This method creates a new image from a portion of the receiving
view. The image is returned autoreleased."*/
{
NSImage
*snapshot = [[NSImage alloc] initWithSize:sourceRect.size];
[snapshot lockFocus];
[[NSColor clearColor] set]; // these two lines may be unnecessary.
NSRectFill(sourceRect);
[self drawRect:sourceRect];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:sourceRect];
[snapshot unlockFocus];
return [snapshot autorelease]; // balance the +alloc call..
}
HTH,
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
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.