Skip to content

Instantly share code, notes, and snippets.

@ArtSabintsev
Created October 14, 2015 18:06
Show Gist options
  • Save ArtSabintsev/7a79ccd22f3bdb827b03 to your computer and use it in GitHub Desktop.
Save ArtSabintsev/7a79ccd22f3bdb827b03 to your computer and use it in GitHub Desktop.
SKTexture Gradient
extension SKTexture {
/* FIXME
This code breaks on iOS 8 (reference post)
http://stackoverflow.com/questions/19243111/spritekit-sktexture-crash/19248293#19248293
*/
convenience init(size: CGSize, firstColor: UIColor, lastColor: UIColor) {
guard let gradientFilter = CIFilter(name: "CILinearGradient") else {
self.init()
return
}
gradientFilter.setDefaults()
let startVector = CIVector(x: size.width/2.0, y: 0)
let endVector = CIVector(x: size.width/2.0, y: size.height)
gradientFilter.setValue(startVector, forKey: "inputPoint0")
gradientFilter.setValue(endVector, forKey: "inputPoint1")
let transformedFirstColor = CIColor(color: firstColor)
let transformedLastColor = CIColor(color: lastColor)
gradientFilter.setValue(transformedFirstColor, forKey: "inputColor1")
gradientFilter.setValue(transformedLastColor, forKey: "inputColor0")
guard let outputImage = gradientFilter.outputImage else {
self.init()
return
}
let context = CIContext()
let image = context.createCGImage(outputImage, fromRect: CGRect(x: 0, y: 0, width: size.width, height: size.height))
self.init(CGImage: image)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment