使用图像时Retina显示问题

hoc*_*man 5 macos objective-c nsimage retina-display

我在视网膜显示器上工作时遇到了问题.NSImage大小是正确的,但如果我从它创建NSBitmapImageRep并将其写入文件我得到的图像女巫的大小是原始图像的两倍大.当我在非视网膜显示器上使用它时没有这样的问题.

  • 我从文件创建NSImage(1920x1080)
  • 我做了一些图纸
  • 我用图纸从图像创建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];
Run Code Online (Sandbox Code Playgroud)

如果我打开图像并保存而不进行编辑,我会得到正确的尺寸图像

NSImage *imageFromFile = [[NSImage alloc] initWithContentsOfURL:fileURL];
NSBitmapImageRep *newRepresentation = [[NSBitmapImageRep imageRepsWithData:[imageFromFile TIFFRepresentation]];
NSData *savingData = [newRepresentation representationUsingType: NSPNGFileType properties: nil];
[savingData writeToFile:desiredFileLocationAndName atomically:no];
Run Code Online (Sandbox Code Playgroud)

Rya*_*los 1

图像的位图表示以像素为单位进行测量。它的尺寸是原来的两倍。NSImage 为您提供了一个以点为单位的大小,在视网膜设备上每点测量 2 个像素。它给你的东西并没有什么问题。