1

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.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
  • See if this helps in any way : http://stackoverflow.com/a/19572286 EDIT: Oh , I just read that you want to capture non-displayed scene... Then this link probably wont work.... But it could be worth of reading – Whirlwind May 31 '16 at 21:28

1 Answers1

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;
}
MoDJ
  • 4,309
  • 2
  • 30
  • 65