我在 Retina 显示器上工作时遇到了问题。 NSImage 大小是正确的,但是如果我从它创建 NSBitmapImageRep 并将其写入文件,我得到的图像大小是原始图像的两倍。当我在非视网膜显示器上使用它时没有这个问题。
- 我从文件 (1920x1080) 创建 NSImage
- 我画了一些画
- 我用绘图的图像创建 NSBitmapImageRep
- 我将它写入文件
- 我得到尺寸为 3840x2160 的图片
是什么原因造成的?
NSImage *originalImage = [[NSImage alloc] initWithContentsOfURL:fileUrl];
NSImage *editedImage = [[NSImage alloc] initWithSize:originalImage.size];
[editedImage lockFocus];
//I draw here NSBezierPaths
[editedImage unlockFocus];
NSBitmapImageRep *savingRep = [NSBitmapImageRep imageRepsWithData:[editedImage TIFFRepresentation]];
NSData *savingData = [savingRep representationUsingType: NSPNGFileType properties: nil];
[savingData writeToFile:desiredFileLocationAndName atomically:no];
如果我打开图像并在不编辑的情况下保存它,我会得到正确尺寸的图像
NSImage *imageFromFile = [[NSImage alloc] initWithContentsOfURL:fileURL];
NSBitmapImageRep *newRepresentation = [[NSBitmapImageRep imageRepsWithData:[imageFromFile TIFFRepresentation]];
NSData *savingData = [newRepresentation representationUsingType: NSPNGFileType properties: nil];
[savingData writeToFile:desiredFileLocationAndName atomically:no];
最佳答案
图像的位图表示以像素为单位。它是两倍大。 NSImage 为您提供以点为单位的大小,在视网膜设备上每点测量 2 个像素。它给你的东西没有错。
关于objective-c - 处理图像时出现 Retina 显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500680/