如何获取NSView的“屏幕截图”?

时间:2022-10-15 02:44:30

I need to take the contents of an NSView and put them in an NSImage, for an experimental project. Is this possible? I did some Googling, tried two methods that I found - but they didn't really work. Any suggestions?

我需要获取NSView的内容并将它们放在NSImage中,用于实验项目。这可能吗?我做了一些谷歌搜索,尝试了两种我发现的方法 - 但它们并没有真正起作用。有什么建议么?

4 个解决方案

#1


21  

[[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]];

#2


14  

From WWDC 2012 Session 245 (translated to Swift):

从WWDC 2012 Session 245(翻译为Swift):

let viewToCapture = self.window!.contentView!
let rep = viewToCapture.bitmapImageRepForCachingDisplayInRect(viewToCapture.bounds)!
viewToCapture.cacheDisplayInRect(viewToCapture.bounds, toBitmapImageRep: rep)

let img = NSImage(size: viewToCapture.bounds.size)
img.addRepresentation(rep)

#3


6  

let dataOfView = view.dataWithPDFInsideRect(view.bounds)
let imageOfView = NSImage(data: dataOfView)

#4


3  

I thinkt, Debashis is thinking more of things like http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208-BCIIIJFB

我想,Debashis正在考虑更多的东西,如http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208- BCIIIJFB

Greetings

#1


21  

[[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]];

#2


14  

From WWDC 2012 Session 245 (translated to Swift):

从WWDC 2012 Session 245(翻译为Swift):

let viewToCapture = self.window!.contentView!
let rep = viewToCapture.bitmapImageRepForCachingDisplayInRect(viewToCapture.bounds)!
viewToCapture.cacheDisplayInRect(viewToCapture.bounds, toBitmapImageRep: rep)

let img = NSImage(size: viewToCapture.bounds.size)
img.addRepresentation(rep)

#3


6  

let dataOfView = view.dataWithPDFInsideRect(view.bounds)
let imageOfView = NSImage(data: dataOfView)

#4


3  

I thinkt, Debashis is thinking more of things like http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208-BCIIIJFB

我想,Debashis正在考虑更多的东西,如http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208- BCIIIJFB

Greetings