2015-11-08 74 views
0

我正在改變我正在寫的遊戲中某些精靈的色調。色調變化的功能是 -在後臺線程中更改SKSpriteNode紋理的色調

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

解決方法是把紋理變化成SKAction -

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

的spriteKit引擎處理在其自己的時間......

相關問題