我正在更改所写游戏中某些精灵的色相。色相改变功能是-

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)
        }
    }
}


如果有人对如何更好地做到这一点有任何建议,我将非常高兴。

谢谢

最佳答案

解决方法是将纹理更改放入SKAction中-

GCD.async
{
    if let colouredImage = self.image(fromOriginalImage: image, withHue: hue)
    {
        let changeTexture = SKAction.setTexture(SKTexture(image: colouredImage))
        self.runAction(changeTexture)
    }
}


spriteKit引擎可以自己处理它。

关于multithreading - 更改背景线程上的SKSpriteNode纹理的色相,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33594239/

10-12 01:35