Skip to content

Instantly share code, notes, and snippets.

@orklann
Forked from kaishin/snapshot.swift
Created December 26, 2020 08:47
Show Gist options
  • Save orklann/d282215f80e05cc653627daa1f632401 to your computer and use it in GitHub Desktop.
Save orklann/d282215f80e05cc653627daa1f632401 to your computer and use it in GitHub Desktop.
NSView Snapshot
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment