Generate an image of the contents of a SKScene which is not displayed

三世轮回 提交于 2019-12-11 04:25:28

问题


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

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