Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 can I access images (not textures) from within an atlas added to xCode project?

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?

like image 986
Alex Stone Avatar asked Dec 19 '13 18:12

Alex Stone


2 Answers

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];
}
like image 69
Deepak Rastogi Avatar answered Nov 07 '22 10:11

Deepak Rastogi


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.

like image 1
Theis Egeberg Avatar answered Nov 07 '22 11:11

Theis Egeberg