Using NSImage in command line tool
Using NSImage in command line tool
- Subject: Using NSImage in command line tool
- From: <email@hidden>
- Date: Tue, 30 Apr 2002 08:50:44 -0500
I am trying to create an image (and later save it to a file) in a small
command line tool, so I created a "Foundation tool" with Project builder
and added references to the AppKit manually. I am not sure if this is OK to
do (I mean, if it's OK to use the AppKit in a tool that is run from the
command line).
My program printed error messages when I tried to lock focus on my newly
created NSImage. Stripped to the very basics, this is what my code looks
like:
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSImage *targetImage = [[NSImage alloc] initWithSize:NSMakeSize
(256,256)];
NSBitmapImageRep *imageRep;
unsigned char *planes;
int i;
// prepare memory for image rep:
planes = malloc (256 * 256 * 3);
for (i=0; i<256*256*3; i++) {
planes[i] = 0;
}
imageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:&planes
pixelsWide:256
pixelsHigh:256
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:256 * 3
bitsPerPixel:24];
[targetImage addRepresentation:imageRep];
// Note: [targetImage isValid] returns YES at this point
// Draw source tiles into target image:
[targetImage lockFocusOnRepresentation:imageRep];
// ... draw code goes here ...
[targetImage unlockFocus];
// ... write image to disk here ...
[targetImage release];
[pool release];
return 0;
}
The error messages that I see are:
2002-04-29 22:02:19.029 GenerateMapTiles[808] _NXCreateWindow: error
creating window (1002)
2002-04-29 22:02:19.382 GenerateMapTiles[808] _initWithWindowNumber: error
creating graphics ctxt object for ctxt:0, window:-1
2002-04-29 22:02:19.397 GenerateMapTiles[808] *** Assertion failure in
-[NSView lockFocus], NSView.m:2084
2002-04-29 22:02:19.397 GenerateMapTiles[808] *** Uncaught exception:
<NSInternalInconsistencyException> lockFocus sent to a view whose window is
deferred and does not yet have a corresponding platform window
This happens when the lockFocusOnRepresentation: message is sent. I tried
using just [targetImage lockFocus] (with no specific representation)
before, with similar results. I am not sure what the view is that is
mentioned in the last line of error messages. Since I am just writing a
tool (as opposed to an application, in ProjectBuilder terms), I thought
there was no view. Maybe that's the problem? Would I be better of creating
a Cocoa application with a window and move my code there?
Thanks for your help!
_______________________________________________
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.