Skip to content

Instantly share code, notes, and snippets.

@kiichi
Created January 5, 2016 04:12
Show Gist options
  • Save kiichi/0a028db62f438d890fb7 to your computer and use it in GitHub Desktop.
Save kiichi/0a028db62f438d890fb7 to your computer and use it in GitHub Desktop.
Example to capture the NSView screenshot in Mac App
import Cocoa
class ScreenshotViewController: NSViewController {
override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
if let viewController = segue.destinationController as? PreviewViewController{
viewController.image = getScreenshot()
}
}
func getScreenshot() -> NSImage {
let rep = self.view.bitmapImageRepForCachingDisplayInRect(self.view.bounds)!
self.view.cacheDisplayInRect(self.view.bounds, toBitmapImageRep: rep)
let image: NSImage = NSImage()
image.addRepresentation(rep)
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment