0

I am trying to draw and save a simple image, but it is ultimately saved twice as big on a MBP RD device:

NSImage* image = [[NSImage alloc] initWithSize:size];
[image lockFocus];

[[NSColor blueColor] set];
NSRectFill(CGRectMake(0, 0, 100, 100));

[image unlockFocus];

// ... then save the image

// Cache the reduced image
NSData *imageData = [self TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
[imageData writeToFile:fileName atomically:NO];

// image is saved as 200 x 200 :(

I would like to have a consistent size, so 100x100 on all devices

Mazyod
  • 22,319
  • 10
  • 92
  • 157

1 Answers1

1

Why would you like a consistent pixel size on all devices? If you keep the pixels the same the viewed size will smaller on a Retina display. If you keep the points the same the viewed size will be the same on either display, but the underlying number of pixels will quadruple. Depending on the application either choice can be valid.

In introducing the Retina display Apple redefined some, higher-level, APIs to take sizes in points; while others, lower-level, ones operate in pixels. If your app uses text, controls, vector graphics etc. there is a good chance that it will work without change on Retina displays. If you use bitmap graphics it may also work, but your images may be a bit blurry (as OS X manufactures the extra pixels need for the Retina resolution).

In your case as you've found out your 100 x 100 has been taken to be points. I can't imagine that you application contains just this one image, so if you wish to operate at the pixel level there are undoubtedly other places you'll be bitten by this. What you need to do is read through Apple's docs, High Resolution Guidelines for OS X is a good place to start. In there you'll see there is a section "Converting Coordinates" which might be particularly applicable, but start at the beginning ;-)

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • I appreciate your clear cut explanation about points and pixels, but as you see I am saving a PNG image. The app I'm making should save for both screens, ultimately, so that's my reasoning. I'll look at the guidelines and see what can be done. – Mazyod Jun 15 '13 at 22:58
  • It doesn't really matter that you producing a PNG - your 100x100 has been interpreted as points rather than pixels due to the changes for retina. You'll find what you need in those docs to produce the two images you need, and you may able able to output them as a single multi- resolution TIFF file. Have fun! – CRD Jun 16 '13 at 02:03