Does anyone know how to "snapshot" a complete SKView or SKScene into an NSImage?
We have been able to use the textureFromNode API to create an SKTexture from a node and all its children. But so far we can't figure out a way to extract the image data into say an NSImage. We are building a mixed Cocoa / SpriteKit app where we need to extract small thumbnails of scenes.
Again, this seems possible on iOS (to get a UIImage) using drawViewHierarchyInRect:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But how to do this on Cocoa with NSImage? Help.
Here was my attempt (on OSX) to scoop the underlying rendered image...
First, I subclassed
SKViewand in itsdrawRectmethod lazily grabbed anNSBitmapImageRepI then set up an
NSTimerwhich would periodically attempt to copy the contents of that cache to a custom thumbnail view:No dice. All I get in my thumbNail view is black.
Likely for the same reason that this is all OpenGL/GPU-based drawing.
That would lead me in the direction of somehow finding the underlying OpenGL drawing context used by
SKViewand doing aglReadPixelson that context.