问题
In my current SKScene, I use UIGraphicsGetImageFromCurrentImageContext
to generate an image from the current graphics on screen.
However, I would like to generate an image from a scene not on screen. One I have created but have not displayed. Is this possible?
The reason for doing is this is to create a custom image for users to share when they achieve a high score, which is similar, but not the same as my main Scene.
回答1:
Here is a method that captures the contents of a node as a PNG. Be aware that current SpriteKit seems to have a memory leak on the access of the CGImage property, so use this in DEBUG mode.
+ (NSData*) captureNodeAsPNG:(SKSpriteNode*)node skView:(SKView*)skView
{
NSData *data = nil;
@autoreleasepool {
SKTexture *captureTexture = [skView textureFromNode:node];
CGImageRef cgImageRef = captureTexture.CGImage;
NSLog(@"capture texture from node as pixels : %d x %d", (int)CGImageGetWidth(cgImageRef), (int)(CGImageGetHeight(cgImageRef)));
UIImage *capImg = [UIImage imageWithCGImage:cgImageRef];
data = UIImagePNGRepresentation(capImg);
}
return data;
}
来源:https://stackoverflow.com/questions/37551796/generate-an-image-of-the-contents-of-a-skscene-which-is-not-displayed