Closed pballada closed 7 years ago
Maybe you mean something like this?
struct GradientFilter: CIImageProcessor {
let startColor: UIColor
let endColor: UIColor
let startVector: CIVector
let endVector: CIVector
var identifier: String {
return "com.domain.CILinearGradient_#{startcolor}_#{endcolor}_#{startvector}_#{endvector}"
}
var filter: Filter {
return Filter {
input in
guard let colorFilter = CIFilter(name: "CILinearGradient") else { return nil }
let startColor = CIColor(cgColor: self.startColor.cgColor)
let endColor = CIColor(cgColor: self.endColor.cgColor)
colorFilter.setValue(self.startVector, forKey: "inputPoint0")
colorFilter.setValue(self.endVector, forKey: "inputPoint1")
colorFilter.setValue(startColor, forKey: "inputColor0")
colorFilter.setValue(endColor, forKey: "inputColor1")
let colorImage = colorFilter.outputImage
let filter = CIFilter(name: "CISourceOverCompositing")!
filter.setValue(colorImage, forKey: kCIInputImageKey)
filter.setValue(input, forKey: kCIInputBackgroundImageKey)
return filter.outputImage?.cropping(to: input.extent)
}
}
}
Please note that the identifier
property should be identical, which says, for every combination of the startColor
, endColor
, startVector
and endVector
, the identifier
should be different. Or Kingfisher will be confused when retrieving the images from cache.
Wow, that was it!!
thanks so much.
Hi, I'm trying to create a gradient generator, using CIImage Filters. I'm using the CIImageProcessor processor wrapper, but I didn't find how can I pass some parameters like the gradient colors and positions.
The code for achieving gradients is like this.
`struct GradientFilter: CIImageProcessor {
}`
I'm calling it like this.
processor = GradientFilter()
But I would like adding some parameters.
processor = GradientFilter(startColor: .red, endColor: .blue, startPosition: CGPoint(x:0,y:0), endPosition: CGPoint(x:0,y:1))
Thanks so much