12

I have a number of atlases (folder.atlas) added to my xCode project, and typically use them to create SKTextures for my sprites by name. However, now I see that I might need to share some images between Sprite Kit and UIView based classes.

    SKTexture* texture = [[SKTextureAtlas atlasNamed:@"Jetpack"] textureNamed:imageName];
//how to get UIImage?

Is there a way for me to get a UIImage out of an atlas at runtime?

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • possible duplicate http://stackoverflow.com/questions/20664804/render-snapshot-spritekit-scene-to-nsimage – CodeSmile Dec 19 '13 at 18:48
  • Not really a duplicate, as the images I'm trying to get are full size images added to atlas, while by taking a screenshot i would get only the pixels visible on screen. – Alex Stone Dec 22 '13 at 19:59

2 Answers2

3

May be it's too late to answer this question but this is a very common issue while we are working on sprite kit. There is no feature, provided by apple to tackle this problem directly. What I've done is working fine for me.

I needed to access any image from atlas dynamite.

NSString *str = imageName;
str = [str stringByReplacingOccurrencesOfString:@".png" withString:@""];

str = [[str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]] componentsJoinedByString:@""];

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:str];

UIImage *atlasImage;
if (atlas.textureNames.count > 1)
{
    SKTexture *texture = [atlas textureNamed:[atlas.textureNames objectAtIndex:0]];

    atlasImage = [UIImage imageWithCGImage:texture.CGImage];
}
1

Yes you can, but I'm warning you, it's not going to be pretty. I don't write out the entire code here because it would take too much time.

If you look in your apps bundle, you'll notice that all your atlas folders are there with an appended "c" (probably for compiled).

If I have an atlas called "graphics.atlas" in my project. In my bundle "Resources" I will then find a "graphics.atlasc" folder. It has two or more files (depending on the size of the atlas, spritekit can split it up).

The files: graphics.1.png graphics.plist

Both can be accessed by any UIKit based code as you please. You'll need to have a look through the plist file though and parse it to find the right place for your image. This is not super easy, but it's definately possible to do.

I'm tempted to write a "SKTextureAtlas+UIImage" class, maybe later.

Theis Egeberg
  • 2,556
  • 21
  • 30
  • 1
    Following Theis Egeberg's advise I did all the above… and got it all to work. You may download my source files from my dropbox at https://www.dropbox.com/s/kwze16rbz7y3f7u/NSImageAtlas.zip. – geowar May 29 '14 at 23:58