3

我正在尝试从另一个 NSImage 的内容创建一个正好为 200 x 300 像素的 NSImage。我不只是缩放,而是从更大的图像中获取它。

生成的图像看起来就像我想要的像素。但是,数量太多了。NSImage 报告的大小为 200 x 300,图像表示报告的大小为 200 x 300,但图像表示报告的像素数是其两倍:400 x 600。当我将此图像表示保存到文件时,我得到400 x 600 的图像。

这是我的做法:

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];

在调试器中,您可以看到 NSBitmapImageRep 的大小正确,但像素数是其两倍。

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

所以,当我将它保存到磁盘时,我得到的图像是 400 x 600,而不是 200 x 300。我该如何解决这个问题?

4

0 回答 0