I am trying to write some code that procedurally creates bitmap fonts, and in order to do that I have been creating SKTextures and then converting them to cgImage / UIImage (because this is necessary to initialise the SKTextureAtlas). However cgImage doesn't seem to work reliably on the simulator whereas it does on the device. The following code illustrates the point if you put it in an SKScene:
let chars = "ABCDEFGHIJKLMNOP"
var y: CGFloat = 0
for each in chars {
let letter = String(each)
let label = SKLabelNode(text: letter)
let view = SKView()
let texture = view.texture(from: label)!
let cgImage = texture.cgImage() // HERE IS WHERE IT GOES WRONG!
let tex2 = SKTexture(cgImage: cgImage) // OR POSSIBLY HERE!
let sprite = SKSpriteNode(texture: tex2)
sprite.position.x = 100
sprite.position.y = y
addChild(sprite)
y += 30
}
On the simulator, some of the characters don't appear (and it's reliably the same ones, strangely) whereas on the device they all show up fine. It's definitely converting it to cgImage and then back to SKTexture which creates the issue, if you remove that the code works fine, but I also get the same issue if I convert the cgImage to UIImage so I think it must be cgImage that has the problem. And such different behaviour on simulator vs device must I think mean a bug? But I've searched and not found anyone else experiencing the same issues. If anyone knows what's going wrong or has a workaround I'd be really grateful.