Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize NSImage in swift? [duplicate]

I want to resize an NSImage from 512px to 60px, I found only the code for iOS, but nothing for OSX.

like image 955
Tomas.1997 Avatar asked Jul 01 '15 07:07

Tomas.1997


1 Answers

I found a function on GitHub, it is working fine for me.

func resize(image: NSImage, w: Int, h: Int) -> NSImage {
    var destSize = NSMakeSize(CGFloat(w), CGFloat(h))
    var newImage = NSImage(size: destSize)
    newImage.lockFocus()
    image.drawInRect(NSMakeRect(0, 0, destSize.width, destSize.height), fromRect: NSMakeRect(0, 0, image.size.width, image.size.height), operation: NSCompositingOperation.CompositeSourceOver, fraction: CGFloat(1))
    newImage.unlockFocus()
    newImage.size = destSize
    return NSImage(data: newImage.TIFFRepresentation!)!
}
like image 186
Sandeep Joshi Avatar answered Sep 23 '22 08:09

Sandeep Joshi