title: Save generated SKTexture to file
body: <blockquote>
    <p>I've now filed a bug for the issue below. Anyone with a good
    workaround?</p>
  </blockquote>
  
  <p>I try to save an <code>SKTexture</code> to file, and load it back again, but I don't succeed. The following code snippet can be copied to <strong>GameScene.m</strong> in the Xcode startup project.</p>
  
  <p>I use <code>textureFromNode</code> in <code>generateTexture</code>, and that seems to be the root cause of my problem. If I use a texture from a sprite, the code works, and two spaceships are visible.</p>
  
  <p>This code worked in iOS 8 but it stopped working in Xcode7 &amp; iOS 9. I just want to verify that this is a bug before I file a bug report. My worry is that I do something wrong with <code>NSKeyedArchiver</code>.</p>
  
  <p>It happens both in simulator and on device.</p>
  
  <pre><code>#import "GameScene.h"
  
  @implementation GameScene
  
  // Generates a texture
  - (SKTexture *)generateTexture
  {
      SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
  
      SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(50, 50)];
      shapeNode.position = CGPointMake(50, 50);
      shapeNode.strokeColor = SKColor.redColor;
      shapeNode.lineWidth = 10;
      [scene addChild:shapeNode];
  
      SKTexture *texture = [self.view textureFromNode:scene];
      //SKTexture *texture = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"].texture; // This works!
  
      return texture;
  }
  
  // Just generate a path
  - (NSString *)fullDocumentsPath
  {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *yourFileName = [documentsDirectory stringByAppendingPathComponent:@"fileName"];
  
      return yourFileName;
  }
  
  - (void)didMoveToView:(SKView *)view
  {    
      self.scaleMode = SKSceneScaleModeResizeFill;
  
      // Verify that the generateTexture method indeed produces a valid texture.
      SKSpriteNode *s1 = [SKSpriteNode spriteNodeWithTexture:[self generateTexture]];
      s1.position = CGPointMake(100, 100);
      [self addChild:s1];
  
      // Start with saving the texture.
      NSString *fullName = [self fullDocumentsPath];
      NSError *error;
      NSFileManager *fileMgr = [NSFileManager defaultManager];
      if ([fileMgr fileExistsAtPath:fullName])
      {
          [fileMgr removeItemAtPath:fullName error:&amp;error];
          assert(error == nil);
      }
      NSDictionary *dict1 = [NSDictionary dictionaryWithObject:[self generateTexture] forKey:@"object"];
      bool ok = [NSKeyedArchiver archiveRootObject:dict1 toFile:fullName];
      assert(ok);
  
      // Read back the texture and place it in a sprite. This sprite is not shown. Why?
      NSData *data = [NSData dataWithContentsOfFile:fullName];
      NSDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
      SKTexture *loadedTexture = [dict2 objectForKey:@"object"];
      SKSpriteNode *s2= [SKSpriteNode spriteNodeWithTexture:loadedTexture];
      NSLog(@"t(%f, %f)", loadedTexture.size.width, loadedTexture.size.height); // Size of sprite &amp; texture is zero. Why?
      s2.position = CGPointMake(200, 100);
      [self addChild:s2];
  }
  
  @end
  </code></pre>
  
  <blockquote>
    <p>Update for Yudong:</p>
  </blockquote>
  
  <p>This might be a more relevant example, but imagine that the scene consists of 4 layers, with lots of sprites. When the game play is over I want to store a thumbnail image of the end scene of the match. The image will be used as a texture on a button. Pressing that button will start a replay movie of the match. There will be lots of buttons with images of old games so I need to store each image on file. </p>
  
  <pre><code>-(SKTexture*)generateTexture
  {
    SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
  
    SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    ship.position = CGPointMake(50, 50);
    [scene addChild:ship];
  
    SKTexture *texture = [self.view textureFromNode:scene];
  
    NSLog(@"texture: %@", texture);
  
    return texture;
  }
  </code></pre>
  
  <blockquote>
    <p>The solution/work around:</p>
  </blockquote>
  
  <p>Inspired by Russells code I did the following. It works!</p>
  
  <pre><code>CGImageRef  cgImg = texture.CGImage;
  SKTexture *newText = [SKTexture textureWithCGImage:cgImg];
  </code></pre>