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"))[!
[]
This solution isn't working either SKTextureAtlas nor SKTextureAtlas Any idea what did I missed, how to make it happen?
Cheers, Szymon