Skip to content

Instantly share code, notes, and snippets.

@DianQK
Created April 27, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DianQK/d688d0ef9b07495a269e4d33958fe805 to your computer and use it in GitHub Desktop.
Save DianQK/d688d0ef9b07495a269e4d33958fe805 to your computer and use it in GitHub Desktop.
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