Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created June 7, 2015 19:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericdke/0ebe314faba67b68ca47 to your computer and use it in GitHub Desktop.
Save ericdke/0ebe314faba67b68ca47 to your computer and use it in GitHub Desktop.
Swift: create PNG from NSView
func makePNGFromView(view: NSView) {
var rep = view.bitmapImageRepForCachingDisplayInRect(view.bounds)!
view.cacheDisplayInRect(view.bounds, toBitmapImageRep: rep)
if let data = rep.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:]) {
data.writeToFile("/xxx/image.png", atomically: false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment