Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retina display problems when working with images

I have met problems when working on retina display. NSImage size is correct, but if I create NSBitmapImageRep from it and write it to file I get image witch's size is twice as big as original image. There is no such problem when I use it on non retina display.

  • I create NSImage from file (1920x1080)
  • I do some drawings on
  • I create NSBitmapImageRep from image with drawings
  • I write it to file
  • I get image with 3840x2160 dimensions

What could cause that?


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];

If I open image and save it without editing I get the correct dimensions image

NSImage *imageFromFile = [[NSImage alloc] initWithContentsOfURL:fileURL];
NSBitmapImageRep *newRepresentation = [[NSBitmapImageRep imageRepsWithData:[imageFromFile TIFFRepresentation]];
NSData *savingData = [newRepresentation representationUsingType: NSPNGFileType properties: nil];
[savingData writeToFile:desiredFileLocationAndName atomically:no];
like image 549
hockeyman Avatar asked Mar 19 '13 13:03

hockeyman


1 Answers

A bitmap representation of the image is measured in pixels. It is twice the size. The NSImage is giving you a size in points which on retina devices measure 2 pixels per point. There is nothing wrong with what its giving you.

like image 103
Ryan Poolos Avatar answered Sep 22 '22 08:09

Ryan Poolos