How do I take a “screenshot” of an NSView?

雨燕双飞 提交于 2019-11-26 19:55:06

问题


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?


回答1:


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



回答2:


From WWDC 2012 Session 245 (translated to 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:


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



回答4:


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

Greetings



来源:https://stackoverflow.com/questions/3251261/how-do-i-take-a-screenshot-of-an-nsview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!