Re: Drawing a view inside an image
Re: Drawing a view inside an image
- Subject: Re: Drawing a view inside an image
- From: Simone Manganelli <email@hidden>
- Date: Mon, 9 Dec 2002 18:39:20 -0800
I was using init, and then I was using the setFrame: method immediately
after, but apparently it still didn't work.
I tried to modify my code to use the method you used below, but now I'm
getting some uncaught exceptions. Here's the new code:
NSProgressIndicator *theProgressIndicator;
NSBitmapImageRep *rep;
NSImage *theNewImage = [[NSImage alloc]
initWithSize:NSMakeSize(60,12)];
NSRect theRect = NSMakeRect(0,0,60,12);
[theProgBarImage release];
theProgressIndicator = [[NSProgressIndicator alloc]
initWithFrame:theRect];
[theProgressIndicator setIndeterminate:NO];
[theProgressIndicator setMaxValue:[theMaxValue doubleValue]];
[theProgressIndicator setDoubleValue:[memUsage doubleValue]];
[theProgressIndicator stopAnimation:nil];
[theProgressIndicator lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:theRect];
[theProgressIndicator unlockFocus];
[theNewImage addRepresentation:rep];
[rep release];
[theProgressIndicator release];
theProgBarImage = theNewImage;
I'm getting this set of errors:
2002-12-09 18:34:15.881 MyApp[18892] *** Assertion failure in
-[NSProgressIndicator lockFocus], AppKit.subproj/NSView.m:2343
2002-12-09 18:34:15.885 MyApp[18892] An uncaught exception was raised
2002-12-09 18:34:15.895 MyApp[18892] lockFocus sent to a view whose
window is deferred and does not yet have a corresponding platform window
2002-12-09 18:34:15.895 MyApp[18892] *** Uncaught exception:
<NSInternalInconsistencyException> lockFocus sent to a view whose
window is deferred and does not yet have a corresponding platform window
MyApp has exited due to signal 5 (SIGTRAP).
Any help?
-- Simone Manganelli
On Monday, Dec 9, 2002, at 16:58 US/Pacific, John C. Randolph wrote:
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.