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?
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];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With