Skip to content

Instantly share code, notes, and snippets.

@zappycode
Created March 30, 2017 20:34
Show Gist options
  • Save zappycode/688ecbe0d851c324dadd4caf9ba5f39f to your computer and use it in GitHub Desktop.
Save zappycode/688ecbe0d851c324dadd4caf9ba5f39f to your computer and use it in GitHub Desktop.
Resize NSImage
func resizeImage(image:NSImage, maxSize:NSSize) -> NSImage {
var ratio:Float = 0.0
let imageWidth = Float(image.size.width)
let imageHeight = Float(image.size.height)
let maxWidth = Float(maxSize.width)
let maxHeight = Float(maxSize.height)
// Get ratio (landscape or portrait)
if (imageWidth > imageHeight) {
// Landscape
ratio = maxWidth / imageWidth;
}
else {
// Portrait
ratio = maxHeight / imageHeight;
}
// Calculate new size based on the ratio
let newWidth = imageWidth * ratio
let newHeight = imageHeight * ratio
// Create a new NSSize object with the newly calculated size
let newSize:NSSize = NSSize(width: Int(newWidth), height: Int(newHeight))
// Cast the NSImage to a CGImage
var imageRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let imageRef = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil)
// Create NSImage from the CGImage using the new size
let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize)
// Return the new image
return imageWithNewSize
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment