Re: Drawing a view inside an image
Re: Drawing a view inside an image
- Subject: Re: Drawing a view inside an image
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 9 Dec 2002 16:58:08 -0800
On Monday, December 9, 2002, at 01:12 PM, Simone Manganelli wrote:
How would one go about drawing the image of an NSView into an NSImage?
Specifically, I want to draw what a specific NSProgressIndicator
looks like into a specific NSImage, which I then want to use in my
program. How would one go about doing that? I've tried it using this
code:
NSProgressIndicator *theProgressIndicator;
NSImage *theProgBarImage = [[NSImage alloc]
initWithSize:NSMakeSize(60,12)];
NSRect theRect = NSMakeRect(0,0,60,12);
[theProgBarImage lockFocus];
theProgressIndicator = [[NSProgressIndicator alloc] init];
[snippage]
However, it doesn't seem to work. What am I doing wrong?
You're calling -init, instead of -initWithFrame: when you create your
progress indicator. The designated initializer for any NSView subclass
is -initWithFrame. The effect of this is that the progress indicator
never created the cell it should use to draw its contents.
BTW, I wrote a category of NSImage a while ago that does this:
@implementation NSView (snapshot)
- (NSImage *) snapshot { return [self snapshotFromRect:[self bounds]]; }
- (NSImage *) snapshotFromRect:(NSRect) sourceRect;
/*"This method creates a new image from a portion of the receiveing
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];
[rep release];
return [snapshot autorelease]; // balance the +alloc call..
}
@end
It's part of the "Color Sampler" example, available at:
http://developer.apple.com/samplecode/Sample_Code/Cocoa/
Color_Sampler.htm
-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.