objective-c - 处理图像时出现 Retina 显示问题

标签 objective-c macos retina-display nsimage

我在 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/

相关文章:

iphone - 表格单元格的奇怪行为

对于配备 Retina 显示屏的 Mac Book Pro,Android 模拟器无法缩放到 100%

ios - 正确显示不是来自图像 Assets 的视网膜图形

objective-c - MKAnnotation 的自定义图像

iphone - UITableView 标题中的 iOS UIButton

objective-c - 来自 NSString 的 Char(十六进制表示)

objective-c - 跨类访问 NSApplicationDelegate 方法

macos - 在 Mac OS X 上安装 OCaml

linux - OS X 是否有与/dev/shm 等价的功能?

css - 视网膜显示屏上的模糊图标