Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
typealias Filter = CIImage -> CIImage
struct RRLazyJunCore {
let filter: Filter
init(filter: Filter = { $0 }) { // 这个就比较尴尬了
self.filter = filter
}
}
extension RRLazyJunCore {
func blur(radius: Double) -> RRLazyJunCore {
return self >>> RRLazyJunCore { image in
let parameters = [
kCIInputRadiusKey: radius,
kCIInputImageKey: image
]
guard let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: parameters) else { fatalError() }
guard let outputImage = filter.outputImage else { fatalError() }
return outputImage
}
}
func cropped(rect: CGRect) -> RRLazyJunCore {
return self >>> RRLazyJunCore { $0.imageByCroppingToRect(rect) }
}
func scale(scale: Double) -> RRLazyJunCore {
return self >>> RRLazyJunCore { image in
let parameters = [
kCIInputImageKey: image,
kCIInputScaleKey: scale,
kCIInputAspectRatioKey: 1
]
guard let filter = CIFilter(name: "CILanczosScaleTransform", withInputParameters: parameters) else { fatalError() }
guard let outputImage = filter.outputImage else { fatalError() }
return outputImage
}
}
}
func >>> (lhs: RRLazyJunCore, rhs: RRLazyJunCore) -> RRLazyJunCore {
return RRLazyJunCore { image in
lhs.filter(rhs.filter(image))
}
}
//RRLazyJunCore()
// .blur(blurRadius)
// .cropped(CGRect(x: 0, y: 0, width: 20, height: 20))
// .scale(3)
// .filter(image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment