如何从SKSpriteNode获取UIImage

问题描述 投票:0回答:1

我从SKSpriteNode获取UIImage对象时遇到了问题,即我可以得到它,但在更改SKSpriteNode的属性如blendMode,colorBlendFactor和color之后,纹理属性保持不变。这是一个例子:

let sprite = SKSpriteNode(imageNamed: "fishka")
sprite.blendMode = .alpha
sprite.colorBlendFactor = 1
sprite.color = UIColor.red
let fishka = UIImage(cgImage: (sprite.texture?.cgImage())!)

这就是我得到的:

result after converting to UIImage (fishka)image in SKSpriteNode object (sprite)

请问,如何在SKSpriteNode中获取带图片的UIImage对象?

谢谢

UPDATE

这是另一个使用CGContext获取所需结果的代码

let img = UIImage(named: "fishka")!
let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
UIGraphicsBeginImageContext(rect.size)
let ctx = UIGraphicsGetCurrentContext()
img.draw(in: rect)
ctx?.setFillColor(UIColor.red.cgColor)
ctx?.setAlpha(1)
ctx?.setBlendMode(.sourceAtop)
ctx?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()

现在图像看起来像this

调整ctx?.setAlpha()不是解决方案,因为我想要一个明亮的红色保持体积view

还有其他解决方案吗?

谢谢* 2

sprite-kit uiimage
1个回答
0
投票

使用SKView.texture(from:SKNode)来实现你想要的。

let sprite = SKSpriteNode(imageNamed: "fishka")
sprite.blendMode = .alpha
sprite.colorBlendFactor = 1

sprite.color = UIColor.red
var fishka = UIImage?
autoreleasepool{
    fishka = scene!.view!.texture(from:sprite)
}
© www.soinside.com 2019 - 2024. All rights reserved.