Programatically creating/saving an NSImage;
Programatically creating/saving an NSImage;
- Subject: Programatically creating/saving an NSImage;
- From: Matt Budd (Madentec) <email@hidden>
- Date: Mon, 21 Mar 2005 14:23:02 -0700
Hello,
I am trying to draw into an NSImage and then save that to a JPG or a TIFF. Here is my code:
float lfWidth = 25;
float lfHeight = 50;
NSImage *loTestImage = [[NSImage alloc] initWithSize: NSMakeSize(lfWidth, lfHeight)];
NSBitmapImageRep *loTestImageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
pixelsWide: lfWidth
pixelsHigh: lfHeight
bitsPerSample: 8
samplesPerPixel: 4
hasAlpha: YES
isPlanar: NO
colorSpaceName: NSDeviceRGBColorSpace
bytesPerRow: 0
bitsPerPixel: 0
];
[loTestImage addRepresentation: loTestImageRep];
[loTestImage lockFocus];
//Here I would do more complicated NSBezierPath stuff
[[NSColor redColor] set];
NSRectFill(NSMakeRect(0, 0, lfWidth, lfHeight));
[loTestImage unlockFocus];
[[loTestImageRep TIFFRepresentation] writeToFile: @"/test.tif" atomically: YES];
[[loTestImageRep representationUsingType: NSJPEGFileType properties: nil] writeToFile: @"/test.jpg" atomically: YES];
[loTestImageRep release];
[loTestImage release];
However, when I run this code, I get two images of the correct size (one TIFF and one JPG), but they don't contain the solid red color, they are random looking pictures. It looks as if I am setting the actual pixels to be some random memory somewhere? Can anyone point me to what I am doing wrong?
- Matt
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden