iossprite-kituiimagesktexturesktextureatlas

iOS, SpriteKit convert SKTextureAtlas/SKTexture into UIImage


I'm trying to retrieve UIImage from SKTexture where SKTexture comes from an SKTextureAtlas. Which make is harder due to the fact .cgImage() isn't working on Atlases.

Anyway, I've came up with solution to use CoreGraphic, however, that seems not to work either. Maybe you are able to help me what did I do wrong in here?

func image(with view: SKView) -> UIImage {
    let format = UIGraphicsImageRendererFormat.default()
    format.scale = UIScreen.main.scale
    format.opaque = false
    let renderer = UIGraphicsImageRenderer(size: view.bounds.size, format: format)
    let image = renderer.image { ctx in
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    }
    return image
}

func image(texture: SKTexture) -> UIImage {
    let view = SKView(frame:CGRect(x: 0, y: 0, width: texture.size().width, height: texture.size().height))
    let scene = SKScene(size: texture.size())
    let sprite  = SKSpriteNode(texture: texture)
    sprite.position = CGPoint(x: view.frame.midX, y: view.frame.midY)
    scene.addChild(sprite)
    view.presentScene(scene)
    return image(with: view)
}

let uiImage = image(texture: SKTexture(imageNamed: "HeavyCrossbow.png"))[!

[enter image description here]

This solution isn't working either SKTextureAtlas nor SKTextureAtlas Any idea what did I missed, how to make it happen?

Cheers, Szymon


Solution

  • Ok, After weekend I finally got it working. Important part is

    guard let render = view.texture(from: sprite) else { return UIImage() }
    return UIImage(cgImage: render.cgImage())
    

    which I got thanks to this answer

    Full working code:

    func image(texture: SKTexture) -> UIImage {
        let view = SKView(frame:CGRect(x: 0, y: 0, width: texture.size().width, height: texture.size().height))
        let scene = SKScene(size: texture.size())
        let sprite  = SKSpriteNode(texture: texture)
        sprite.position = CGPoint(x: view.frame.midX, y: view.frame.midY)
        scene.addChild(sprite)
        view.presentScene(scene)
    
        guard let render = view.texture(from: sprite) else { return UIImage() }
        return UIImage(cgImage: render.cgImage())
    }
    

    The reason I was looking to make it work is that I'm writing game with SpriteKit however UI is done with SwiftUI.