我正在尝试从 SKTexture 检索 UIImage,其中 SKTexture 来自 SKTextureAtlas。由于 .cgImage() 不适用于 Atlases,因此这更难。
无论如何,我想出了使用 CoreGraphic 的解决方案,但是,这似乎也不起作用。也许你能帮助我在这里做错了什么?
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"))[!
[ ]
SKTextureAtlas 和 SKTextureAtlas 这个解决方案都不起作用知道我错过了什么,如何实现它?
干杯,席蒙