Created
April 27, 2016 15:48
-
-
Save dianqk/d688d0ef9b07495a269e4d33958fe805 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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