SKTextureAtlas Error

I am using the following code to create a texture atlas at runtime using a single .png image sprite sheet:

func createSpriteTextureAtlas(atlasNumber atlas:Int, forWorld world:Int) {
        //load the png file
        let image = UIImage(named: "world\(world)_spritesheet\(atlas)_2048x2048.png")
        //create the dictonary
        var imageDictionary = [String: UIImage]()
        //iterate through all rows and columns and get the subimage
        var imageIndex = 0
        for row in 0...7 {
            for column in 0...7 {
                let sourceRect = CGRect(x:column * 256, y:row * 256, width:256, height:256)
                let sourceImage = image?.cgImage!.cropping(to: sourceRect)
                let subImage = UIImage(cgImage: sourceImage!)
                //add the sub image and name to the dictionary
                imageDictionary["\(imageIndex)"] = subImage
                imageIndex = imageIndex + 1
            }
        }
        //create the texture atlas using the dictionary
        spriteTextureAtlas[atlas] = SKTextureAtlas(dictionary: imageDictionary)
    }

I have a different sprite sheet for every world. I made all the sprite sheets myself using the same tool. This code works 100% of the time for most images.

For some images however, the program crashes at: SKTextureAtlas(dictionary: imageDictionary) with the error: Thread 4: EXC_BAD_ACCESS (code=1, address=0x105ff2000). The stack trace says it is crashing inside: #0 0x00000002178e2d34 in -[SKTextureAtlasPacker isFullyOpaque:] ().

The crash does not happen every time and only happens for some images. The crash never happens on the simulator.

Did I make a mistake inside createSpriteTextureAtlas or is this a SpriteKit bug?

P.S. I already know that I can let Xcode make the texture atlas for me by using a folder with a .atlas extension but this is not what i want to do.

  • The crash can be avoided by removing the alpha channel from the sprite sheet but it makes the images look poor. Looks like a SpriteKit bug which I reported to Apple. I asked the same question on StackOverflow and the community agreed that it is a SpriteKit bug.

Add a Comment

Replies

+1 Also having this same issue