从CGImageRef创建NSImage会导致图像像素大小加倍

时间:2014-12-10 13:11:05

标签: objective-c cocoa nsimage cgimageref

capture是从CGImageRef调用返回的CGWindowListCreateImage()。当我尝试通过NSImage将其直接转换为initWithCGImage:size:时,它的大小会神秘地翻倍。如果我改为从NSBitmapImageRep手动创建capture,然后将其添加到空NSImage,那么一切正常。

我的硬件设置是视网膜MBP +非视网膜外部显示器。捕获发生在非视网膜屏幕上。

NSLog(@"capture image size: %d %d", CGImageGetWidth(capture), CGImageGetHeight(capture));
NSLog(@"logical image size: %f %f", viewRect.size.width, viewRect.size.height);

NSBitmapImageRep *debugRep;
NSImage *image;

//
// Create NSImage directly

image = [[NSImage alloc] initWithCGImage:capture size:NSSizeFromCGSize(viewRect.size)];

debugRep = [[image representations] objectAtIndex:0];
NSLog(@"pixel size, NSImage direct: %d %d", debugRep.pixelsWide, debugRep.pixelsHigh);

//
// Create representation manually

NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:capture];
image = [[NSImage alloc] initWithSize:NSSizeFromCGSize(viewRect.size)];
[image addRepresentation:imageRep];
[imageRep release];

debugRep = [[image representations] objectAtIndex:0];
NSLog(@"pixel size, NSImage + manual representation: %d %d", debugRep.pixelsWide, debugRep.pixelsHigh);

日志输出:

capture image size: 356 262
logical image size: 356.000000 262.000000
pixel size, NSImage direct: 712 524
pixel size, NSImage + manual representation: 356 262

这是预期的行为吗?

1 个答案:

答案 0 :(得分:2)

initWithCGImage:size:州的documentation

  

除此之外,你不应该对图像做任何假设   绘制它相当于绘制CGImage

最后,我继续直接使用NSBitmapImageRep实例。