hockeyman
hockeyman

Reputation: 1183

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.

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

Upvotes: 6

Views: 763

Answers (1)

Ryan Poolos
Ryan Poolos

Reputation: 18561

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.

Upvotes: 1

Related Questions