Re: Creating Gray Scale Image give big leak...
Re: Creating Gray Scale Image give big leak...
- Subject: Re: Creating Gray Scale Image give big leak...
- From: Heinrich Giesen <email@hidden>
- Date: Wed, 19 Dec 2007 10:23:16 +0100
Hello,
On 19.12.2007, at 02:33, Jerry LeVan wrote:
I have recast your code into a method...
*******************************************
// Returns an autoreleased grayscale version of
// its parameter theImage
- (NSImage *) monochromeImage:(NSImage*) theImage
{
// get NSImage to bitmaprep
NSBitmapImageRep * abitmap;
abitmap = (NSBitmapImageRep*)[theImage
bestRepresentationForDevice:nil];
int pw = [abitmap pixelsWide];
int ph = [abitmap pixelsHigh];
NSBitmapImageRep * bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL pixelsWide:pw
pixelsHigh:ph bitsPerSample:8 samplesPerPixel:1
hasAlpha:NO isPlanar:NO colorSpaceName:NSCalibratedWhiteColorSpace
bytesPerRow:0 bitsPerPixel:0];
[bitmap setSize: [theImage size]];
NSImage * image =[ [NSImage alloc] initWithSize:[theImage size ] ];
NSGraphicsContext *nsContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: nsContext];
// Do I need a lockFocus here?
[ theImage drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect
operation:NSCompositeCopy fraction:1.0];
// Restore the previous graphics context and state.
[NSGraphicsContext restoreGraphicsState];
[image addRepresentation: bitmap];
[bitmap release];
return [image autorelease];
}
No, you don't need a lockFocus. Setting a new NSGrahpicsContent did
the job.
Let me guess: you didn't test this method with a high resolution
(>>72 dpi) image ?
Setting the size of the new NSBitmapImageRep is done too early. You
have to do it
*after* the drawing/rendering. In my posted version I set the size after
restoring the graphicsState, and there it works well. Drawing/rendering
an image with another resolution than 72 dpi always seems to be a bit
dangerous.
Heinrich
--
Heinrich Giesen
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden