我从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对象?
谢谢
更新
这是另一个使用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
答案 0 :(得分: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)
}