Re: Programatically creating/saving an NSImage;
Re: Programatically creating/saving an NSImage;
- Subject: Re: Programatically creating/saving an NSImage;
- From: Matt Budd (Madentec) <email@hidden>
- Date: Mon, 21 Mar 2005 15:31:43 -0700
Hey Ricky,
Thanks for the reply. I tried substituting that call to -lockFocus with
this code:
[loTestImage lockFocusOnRepresentation: loTestImageRep];
But still no luck. It now appears that the random bytes/pixels are
going, but for the JPG, I only get a solid-black image, and for the
TIFF, it is all white. In neither of them do I get the red background.
And if I try to do more complicated drawing besides just filling a rect
(i.e. by using a NSBezierPath and -fill for a custom path), that
doesn't show up either....it's still either black or white filled...
- Matt
On Mar 21, 2005, at 3:04 PM, Ricky Sharp wrote:
On Mar 21, 2005, at 3:23 PM, Matt Budd (Madentec) wrote:
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?
I'm not sure if the rep that is being locked is the one that you
created.
So, rather than calling lockFocus on the NSImage (which will in turn
lock what it thinks is the best representation), use
lockFocusOnRepresentation to specifically lock the image rep you
created.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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