Created
January 10, 2016 14:29
-
-
Save kaishin/d4df8fc6c701ca635e25 to your computer and use it in GitHub Desktop.
NSView Snapshot
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
extension NSView { | |
var snapshot: NSImage { | |
guard let bitmapRep = bitmapImageRepForCachingDisplayInRect(bounds) else { return NSImage() } | |
bitmapRep.size = bounds.size | |
cacheDisplayInRect(bounds, toBitmapImageRep: bitmapRep) | |
let image = NSImage(size: bounds.size) | |
image.addRepresentation(bitmapRep) | |
return image | |
} | |
} |
Figured it out. You need to change the bitmap representation properties (size in this instance) after setting it as a represenation of our target image. Props to @robrix for pointing me in the right direction!
extension NSView {
var snapshot: NSImage {
guard let bitmapRep = bitmapImageRepForCachingDisplayInRect(bounds) else { return NSImage() }
cacheDisplayInRect(bounds, toBitmapImageRep: bitmapRep)
let image = NSImage()
image.addRepresentation(bitmapRep)
bitmapRep.size = bounds.size.doubleScale()
return image
}
}
extension CGSize {
func doubleScale() -> CGSize {
return CGSize(width: width * 2, height: height * 2)
}
}
How to cache NSView filtered content?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure how to get a retina-scale (@2x) snapshot here. Tried messing with the different sizes but nothing worked...