Resize an NSImage Proportionately in Swift

For the past couple of days, I’ve been working on a small project for OS X written in Swift where it was necessary to import an image and scale it down proportionately based on a given width and height.

Since I couldn’t find much help online, I thought I would contribute my bit and post the function I wrote to do the dirty work. Essentially, the function allows you to pass an NSImage to it as well as an NSSize object containing the size to scale the image to. It then uses these to produce and return a new, proportionately scaled NSImage based on the new width and height.

The code:

//
//  ImageResizer.swift
//
//  Created by Alex Seifert on 18/06/2016.
//  http://www.alexseifert.com
//

import Foundation
import Cocoa

// Function for resizing an NSImage
// Parameters:
//      image:NSImage -> The NSImage that will be resized
//      maxSize:NSSize -> An NSSize object that will define the new size of the image

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 = CGRectMake(0, 0, image.size.width, image.size.height)
    let imageRef = image.CGImageForProposedRect(&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
}

This code is also available as a GitHub Gist.

This entry was posted in Development, Mac, Mac Development and tagged , , , , . Bookmark the permalink. Follow comments with the RSS feed for this postPost a comment or leave a trackback: Trackback URL.

2 Comments

  1. olivier robin
    Posted 16 January 2017 at 12:20 pm | Permalink

    Hi,

    this code does not work :
    the image resized has the same size than the originale one

    Olivier

    • Alex
      Posted 9 May 2017 at 3:55 pm | Permalink

      Can you provide more details? I’ve used this code in a couple of applications without a problem, so I would be interested to know in what sort of context it doesn’t work.

Post a Comment

Your email is kept private. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>