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

4
capture 是通过调用 CGWindowListCreateImage() 返回的 CGImageRef。当我尝试通过 initWithCGImage:size: 直接将其转换为 NSImage 时,它的大小神秘地增加了一倍。如果我手动从 capture 创建一个 NSBitmapImageRep,然后将其添加到一个空的 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

这是期望的行为吗?

似乎你有视网膜图像。 - pronebird
有相同的问题,有更新吗? - Yunus Nedim Mehel
1个回答

2

initWithCGImage:size:的文档说明:

您不应该假设图像除了绘制CGImage之外还有其他任何属性。

最终,我直接使用NSBitmapImageRep实例继续工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接