Search code examples
iosswiftsprite-kitsktexturesktextureatlas

UImage loads entire SKTextureAtlas instead of just SKTexture


I need texture with 'trueName' to load into my myImageViewOutlet.image. The result I am getting is the entire atlas rendering into it. My code is as follows

let myTextureAtlas = SKTextureAtlas.init(named: "myGameTextures")

// let texture = myTextureAtlas.textureNamed(trueName) // will render the entire atlas, instead of just the texture by trueName

let texture = myTextureAtlas.textureNamed(trueName+”bad name“) // will produce the placeholder image by sprite kit by itself just fine, but it's not the one I want obviously

let image = texture.CGImage()

let newImage = UIImage(CGImage: image, scale: 1.0, orientation: UIImageOrientation.Up)
//        let newImage = UIImage(CGImage: image) // .. the straight forward way

myImageViewOutlet.image = newImage

Solution

  • I reconciled image.CGImage() does not support textures from SKTextureAtlas, after reading a similar post SKTextures from SKTextureAtlas does not support mipmapping.

    One approach is to form a SKTexture from SKView.textureFromNode(_ node: SKNode)

    let textureToRender = myOffscreenView.textureFromNode(nodeToRender)
    
    let newImage = UIImage(CGImage: textureToRender.CGImage())