3

I am trying to create an NSImage that is exactly 200 x 300 pixels large from the contents of another NSImage. I'm not just scaling, but taking it from a chunk of a much larger image.

The resulting image looks just like the pixels I want. However, there are too many. The NSImage reports a size of 200 x 300, and the image representations report a size of 200 x 300, but the image representations report a number of pixels twice that : 400 x 600. When I save this image representation to a file, I get an image that's 400 x 600.

Here's how I am doing it:

NSRect destRect = NSMakeRect(0,0,200,300);
NSImage* destImage = [[NSImage alloc] initWithSize:destRect.size];

// lock focus, set interpolation
[destImage lockFocus];
NSImageInterpolation oldInterpolation = [[NSGraphicsContext currentContext] imageInterpolation];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];

[_image drawInRect:destRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0];

[destImage unlockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:oldInterpolation];



NSData* tiffImageData = [destImage TIFFRepresentation];
NSBitmapImageRep* tiffImageRep = [NSBitmapImageRep imageRepWithData:tiffImageData];

In the debugger, you can see the NSBitmapImageRep has the right size, but twice the number of pixels.

po tiffImageRep
(NSBitmapImageRep *) $5 = 0x000000010301ad80 NSBitmapImageRep 0x10301ad80 Size={200, 300} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) **Pixels=400x600** Alpha=YES Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x10f353a40

So, when I save it to disk, I get an image that is 400 x 600, not 200 x 300. How do I fix this?

John A. Vink
  • 301
  • 1
  • 3
  • 15

0 Answers0