gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:41 24 4
gpt4 key购买 nike

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

这是预期的行为吗?

最佳答案

documentation对于 initWithCGImage:size: 状态:

You should not assume anything about the image, other than that drawing it is equivalent to drawing the CGImage.

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

关于objective-c - 从 CGImageRef 创建 NSImage 会导致图像像素大小加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com