【问题标题】:Creating NSImage from CGImageRef causes image pixel size to double从 CGImageRef 创建 NSImage 会导致图像像素大小加倍
【发布时间】:2014-12-10 13:11:04
【问题描述】:

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

这是预期的行为吗?

【问题讨论】:

  • 你好像有视网膜图像
  • 遇到同样的问题,有什么更新吗?

标签: objective-c cocoa nsimage cgimageref


【解决方案1】:

documentationinitWithCGImage:size: 声明:

除此之外,您不应假设有关图像的任何内容 绘制它相当于绘制CGImage

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

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    相关资源
    最近更新 更多