Skip to content

Instantly share code, notes, and snippets.

@rishigesh
Created March 17, 2017 15:24
Show Gist options
  • Save rishigesh/fc66a2cb35372b2aa011a7a69f606d40 to your computer and use it in GitHub Desktop.
Save rishigesh/fc66a2cb35372b2aa011a7a69f606d40 to your computer and use it in GitHub Desktop.
NSImage Flip Horizontally (Swift)
public func flipHorizontally() -> IOSXImage {
let existingImage: NSImage? = self
let existingSize: NSSize? = existingImage?.size
let newSize: NSSize? = NSMakeSize((existingSize?.width)!, (existingSize?.height)!)
let flipedImage = NSImage(size: newSize!)
flipedImage.lockFocus()
let t = NSAffineTransform.init()
t.translateX(by: (existingSize?.width)!, yBy: 0.0)
t.scaleX(by: -1.0, yBy: 1.0)
t.concat()
let rect:NSRect = NSMakeRect(0, 0, (newSize?.width)!, (newSize?.height)!)
existingImage?.draw(at: NSZeroPoint, from: rect, operation: .sourceOver, fraction: 1.0)
flipedImage.unlockFocus()
return flipedImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment