我正在尝试从另一个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的图像。我该如何解决这个问题?