有谁知道如何将完整的 SKView
或 SKScene
“快照”到 NSImage
中?
我们已经能够使用 textureFromNode
API 从一个节点及其所有子节点创建一个 SKTexture
。但到目前为止,我们还没有找到一种方法将图像数据提取到 NSImage
中。我们正在构建一个混合的 Cocoa
/SpriteKit
应用程序,我们需要在其中提取场景的小缩略图。
同样,这似乎可以在 iOS 上使用 drawViewHierarchyInRect
(获取 UIImage
):
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但是如何使用 NSImage
在 Cocoa
上执行此操作?帮助。
最佳答案
自 OS X 10.11(以及 iOS 9 和 tvOS,但我们将忽略它们,因为您询问的是 NSImage
),SKTexture
可以将其内容导出到一个 CGImageRef
。因此,您提到的 textureFromNode
方法现在是可行解决方案的第一步:
SKTexture *texture = [view textureFromNode: view.scene];
NSImage *image = [[NSImage alloc] initWithCGImage: texture.CGImage
size: view.bounds];
请注意 initWithCGImage 的
您可以将 size
参数:CGSizeZero
传递到那里以使 NSImage
继承 <CGImage
的 em>pixel 大小,但由于您可能正在处理 Retina 显示器,因此您可能需要 point 大小,您可以从 View 中获取该大小维度。
关于objective-c - 渲染/快照 SpriteKit 场景到 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664804/