Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SKaplanOfficial/b3cbf80305c1de6ed1a5d275e3e1458e to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/b3cbf80305c1de6ed1a5d275e3e1458e to your computer and use it in GitHub Desktop.
ASObjC handler to resize an NSImage to specific pixel dimensions rather than points.
on resize(sourceImage, newSize)
set rep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(newSize's width) pixelsHigh:(newSize's height) bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:no colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0
rep's setSize:newSize
current application's NSGraphicsContext's saveGraphicsState()
current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:rep)
sourceImage's drawInRect:(current application's NSMakeRect(0, 0, newSize's width, newSize's height)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:1.0
current application's NSGraphicsContext's restoreGraphicsState()
set newImage to current application's NSImage's alloc()'s initWithSize:newSize
newImage's addRepresentation:rep
return newImage
end resize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment