rss_feed

Resize an NSImage Proportionately in Swift

18 June 2016

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:

This code is available here as a GitHub Gist.

How would you rate this post?

About the Author

Alex Seifert
Alex is a developer, a drummer and an amateur historian. He enjoys being on the stage in front of a large crowd, but also sitting in a room alone, programming something or writing about history.

Related Posts

2 Comments
  1. olivier robin
    16 January 2017 12:20 pm  link

    Hi,

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

    Olivier

    • Alex
      9 May 2017 3:55 pm  link

      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>