1

I would like to be able to export my SKTexture to an external image file. I have been able to achieve this using the following code. However it suffers from the critical flaw that the alpha channel is ignored even thoough the view is not opaque and all background colours are set to clear.

- (void)export:(CGRect)bounds texture:(SKTexture *)texture path:(NSString *)path{
    UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
    SKView *view = [[SKView alloc] initWithFrame:bounds];
    SKScene *scene = [SKScene sceneWithSize:CGSizeMake(bounds.size.width, bounds.size.height)];

    view.opaque = NO;
    view.backgroundColor = [UIColor clearColor];
    scene.backgroundColor = [UIColor clearColor];

    SKSpriteNode *node = [SKSpriteNode spriteNodeWithTexture:texture];
    node.position = CGPointMake(bounds.size.width/2, bounds.size.height/2);
    [scene addChild:node];
    [view presentScene:scene];
    [view drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [UIImagePNGRepresentation(snapshotImage) writeToFile:path atomically:YES];
}

Is there any alterations I could make the current solution to allow exporting images that include the alpha value?

Is there some more obvious approach to exporting textures to an image?

Intention: The textures I am exporting are currently generated on game load from SKShapeNode's in order to work around SKShapeNode's pitfalls. To avoid texture generation overhead I would like to export them as images and instead load these textures directly from a texture atlas.

nacross
  • 2,013
  • 2
  • 25
  • 37
  • It might be worth noting, that I can use private API if necessary as this code will not be submitted to the App Store. – nacross May 18 '14 at 07:45
  • create the shapes as images and use sprites - you could use a vector graphics program to create shapes on your computer. The overhead of turning shapes into textures is still significant, if not more than loading textures. And not being able to use a texture atlas with runtime-created textures will negatively affect render performance. – CodeSmile May 18 '14 at 08:45
  • Thanks for the suggestion, I am definitely aware I could avoid the problem by just not trying to do it :-) I was hoping to avoid answers like this though with my stated intention being exporting existing shape nodes so that I can in future load them via a texture atlas. The point is I have already created the graphics I wanted to use and didn't want to recreate them using an external graphics editor. – nacross May 18 '14 at 09:32
  • what if you display each shape on screen, take a screenshot (off button + home) and cut out the shape on your computer? – CodeSmile May 18 '14 at 11:10
  • Unfortunately the alpha value is not only either 1 or 0, which means cutting out isn't really a good option. Some textures were generated using SKEffectNode(s) using things like blur filters, which do not leave hard edges to cut. – nacross May 18 '14 at 11:17

1 Answers1

1

There doesn't appear to be anyway to achieve transparent background for exported textures.

According to this SO question SKScene cannot be transparent and the likely cause is that SpriteKit uses a framebuffer that doesn't allow for this transparency for performance reasons.

Community
  • 1
  • 1
nacross
  • 2,013
  • 2
  • 25
  • 37
  • I haven't been able to find anyway to do this in iOS7.1 – nacross May 24 '14 at 05:33
  • It might be worth noting that as of iOS8 the introduction of the [allowsTransparency](https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/#//apple_ref/occ/instp/SKView/allowsTransparency) method seems to indicate transparency may be possible, I have not tested this though. – nacross Oct 09 '14 at 10:30