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
这是期望的行为吗?