I do some operations on images, and after I finish, I want to save the image as PNG to disk. I do the following:
+ (void)saveImage:(NSImage *)image atPath:(NSString *)path { [image lockFocus] ; NSBitmapImageRep *imageRepresentation = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0.0, 0.0, image.size.width, image.size.height)] ; [image unlockFocus] ; NSData *data = [imageRepresentation representationUsingType:NSPNGFileType properties:nil]; [data writeToFile:path atomically:YES]; }
This code works, but the problem is with the mac retina, if I print the NSBitmapImageRep object, I get a different size and a straight pixel, and when my image is saved to disk, it is twice as large:
$0 = 0x0000000100413890 NSBitmapImageRep 0x100413890 Size={300, 300} ColorSpace=sRGB IEC61966-2.1 colorspace BPS=8 BPP=32 Pixels=600x600 Alpha=YES Planar=NO Format=0 CurrentBacking=<CGImageRef: 0x100414830>
I tied to make the pixel size not care about the scale of the retina, since I want to keep the original size:
imageRepresentation.pixelsWide = image.size.width; imageRepresentation.pixelsHigh = image.size.height;
This time I get the correct size when I print the NSBitmapImageRep object, but when I save the file, I still get the same problem:
$0 = 0x0000000100413890 NSBitmapImageRep 0x100413890 Size={300, 300} ColorSpace=sRGB IEC61966-2.1 colorspace BPS=8 BPP=32 Pixels=300x300 Alpha=YES Planar=NO Format=0 CurrentBacking=<CGImageRef: 0x100414830>
Any idea how to fix this and keep the original pixel size?
retina-display image-resizing macos nsimage nsbitmapimagerep
Ludovic Landry Jul 06 '13 at 21:08 2013-07-06 21:08
source share