Search code examples
iosswiftdictionarysktextureatlas

Creating an SKTextureAtlas from a dictionary


If an iOS project has a dictionary of UIImages, how can an SKTextureAtlas be properly created from the dictionary images at runtime? I haven't been able to accomplish this goal, though Apple's SKTextureAtlas class reference suggests the following when declaring the atlas: convenience init!(dictionary properties: [NSObject : AnyObject]).

For example, how can the following code be completed or reworked to create an SKTextureAtlas from the UIImages in tempDictionary?

var strawberryDrawing:UIImage = UIImage(named: "strawberry")!
var bananaDrawing:UIImage = UIImage(named: "banana")!

var tempDictionary:[String:UIImage] = ["First Fruit":strawberryDrawing, "Second Fruit":bananaDrawing]

var atlasOfFruitDrawings:SKTextureAtlas = ???

Solution

  • let strawberryDrawing = UIImage(named: "strawberry")!
    let bananaDrawing = UIImage(named: "banana")!
    let tempDictionary: [NSObject:AnyObject] = ["First Fruit":strawberryDrawing, "Second Fruit":bananaDrawing]
    let atlasOfFruitDrawings = SKTextureAtlas(dictionary: tempDictionary)