Created
January 5, 2016 04:12
-
-
Save kiichi/0a028db62f438d890fb7 to your computer and use it in GitHub Desktop.
Example to capture the NSView screenshot in Mac App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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