I am trying to make a gradient and use it as an alpha mask. Right now, I can make an image like this (black to transparent):

This is the code that I use to create all of this:
private func createImage(width: CGFloat, height: CGFloat) -> CGImageRef?{
if let ciFilter = CIFilter(name: "CILinearGradient"){
let ciContext = CIContext()
ciFilter.setDefaults()
let startColor = CIColor(red: 0, green: 0, blue: 0, alpha: 0)
let endColor = CIColor(red: 0, green: 0, blue: 0, alpha: 1)
let startVector = CIVector(x: 0, y: height-10)
let endVector = CIVector(x: 0, y: height-22)
ciFilter.setValue(startColor, forKey: "inputColor0")
ciFilter.setValue(endColor, forKey: "inputColor1")
ciFilter.setValue(startVector, forKey: "inputPoint0")
ciFilter.setValue(endVector, forKey: "inputPoint1")
if let outputImage = ciFilter.outputImage {
let cgImage:CGImageRef = ciContext.createCGImage(outputImage, fromRect: CGRect(x: 0, y: 0, width: width, height: height))
return cgImage
}
}
return nil
}
For me, this method works perfectly, because I only create a mask in my code (when the GUI element is created), so the performance does not change at all.
The thing is, I really need to get the resulting image (gradient texture) so that it looks (gradients should have a fixed height, but the size of the black area can vary):

CILinearGradient inputImage, . , .
, . ( , , ), .
, - CILinearGradient , ? , ?
, SpriteKit CAGradientLayer, , .