Skip to content

Instantly share code, notes, and snippets.

@mattstevens
Last active March 9, 2022 12:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattstevens/4400775 to your computer and use it in GitHub Desktop.
Save mattstevens/4400775 to your computer and use it in GitHub Desktop.
Resizing an NSImage on retina Macs for output as a 1x image
NSImage *computerImage = [NSImage imageNamed:NSImageNameComputer];
NSInteger size = 256;
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:size
pixelsHigh:size
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0];
[rep setSize:NSMakeSize(size, size)];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:rep]];
[computerImage drawInRect:NSMakeRect(0, 0, size, size) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
NSData *data = [rep representationUsingType:NSPNGFileType properties:nil];
@lvandal
Copy link

lvandal commented Mar 23, 2013

Cool! And how would you do it so that you get a 2x image instead?

@younthu
Copy link

younthu commented Jan 6, 2015

I want to know how to get a 2x image as also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment