Last active
March 9, 2022 12:53
-
-
Save mattstevens/4400775 to your computer and use it in GitHub Desktop.
Resizing an NSImage on retina Macs for output as a 1x image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to know how to get a 2x image as also.