我在写作的游戏中改变了一些精灵的色调。色调更改功能是 -
private func image(fromOriginalImage image: UIImage, withHue hue: CGFloat) -> UIImage
{
let rect = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: image.size)
UIGraphicsBeginImageContext(image.size)
let context = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(context, 0.0, image.size.height)
CGContextScaleCTM(context, 1.0, -1.0)
CGContextDrawImage(context, rect, image.CGImage)
CGContextSetBlendMode(context, CGBlendMode.Hue)
CGContextClipToMask(context, rect, image.CGImage)
CGContextSetFillColorWithColor(context, UIColor(hue: hue, saturation: 0.5, brightness: 0.4, alpha: 1.0).CGColor)
CGContextFillRect(context, rect)
let colouredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return colouredImage
}
这在后台线程上工作正常,然后我将它返回的图像带回主线程以更新SKSpriteNode的纹理,&偶尔我会在游戏中出现明显的口吃 -
GCD.async
{
if let colouredImage = self.image(fromOriginalImage: image, withHue: hue)
{
GCD.async_main
{
self.texture = SKTexture(image: colouredImage)
}
}
}
如果有人建议如何更好地做到这一点,我会非常高兴。
由于
答案 0 :(得分:0)
修复方法是将纹理更改放入SKAction -
GCD.async
{
if let colouredImage = self.image(fromOriginalImage: image, withHue: hue)
{
let changeTexture = SKAction.setTexture(SKTexture(image: colouredImage))
self.runAction(changeTexture)
}
}
spriteKit引擎在它自己的时间处理它......