Frage

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

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top