6

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];
hockeyman
  • 1,141
  • 6
  • 27
  • 57

1 Answers1

1

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.

Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • Is there something in particular you need to do that the new dimensions are making difficult? – Ryan Poolos Mar 19 '13 at 13:25
  • The problem is that when I open image and process it with my app, then when I save image I get it twice as big as it was before in pixels. And I need to maintain original image size. – hockeyman Mar 19 '13 at 13:34
  • Can you post the code you're using for each of those steps. I think there is a flag you can set to avoid that issue. – Ryan Poolos Mar 19 '13 at 13:38
  • I have noticed that without doing any drawings I get correct image dimensions. – hockeyman Mar 19 '13 at 14:10