14

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

4

4 回答 4

26

来自 WWDC 2012 Session 245(翻译成 Swift):

let viewToCapture = self.window!.contentView!
let rep = viewToCapture.bitmapImageRepForCachingDisplay(in: viewToCapture.bounds)!
viewToCapture.cacheDisplay(in: viewToCapture.bounds, to: rep)

let img = NSImage(size: viewToCapture.bounds.size)
img.addRepresentation(rep)
于 2016-06-08T12:49:15.770 回答
23
[[NSImage alloc] initWithData:[view dataWithPDFInsideRect:[view bounds]]];
于 2010-07-15T01:02:18.330 回答
6
let dataOfView = view.dataWithPDFInsideRect(view.bounds)
let imageOfView = NSImage(data: dataOfView)
于 2015-05-10T02:47:04.427 回答
3

我认为,Debashis 正在考虑更多类似http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Images/Images.html#//apple_ref/doc/uid/TP40003290-CH208- BCIIIJFB

问候

于 2010-07-15T09:09:31.903 回答