组合滤镜
熟悉下函数式编程思想
下面BFilter里面代码参考自functional-swift一书
BFilter.swift
import UIKit
// MARK: - 组合滤镜 - 事例
typealias Filter = CIImage -> CIImage //构建了Filter 类型
struct BFilter {
// 模糊 - 高斯模糊
func blur(radius: Double) -> Filter {
return { 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 colorGenerator(color: UIColor) -> Filter {
return { _ in
let c = CIColor(color: color)
let parameters = [kCIInputColorKey: c]
guard let filter = CIFilter(name: "CIConstantColorGenerator", withInputParameters: parameters) else {
fatalError()
}
guard let outputImage = filter.outputImage else {
fatalError()
}
return outputImage
}
}
func compositeSourceOver(overlay: CIImage) -> Filter {
return { image in
let parameters = [
kCIInputBackgroundImageKey: image,
kCIInputImageKey: overlay
]
guard let filter = CIFilter(name: "CISourceOverCompositing", withInputParameters: parameters) else {
fatalError()
}
guard let outputImage = filter.outputImage else {
fatalError()
}
let cropRect = image.extent
return outputImage.imageByCroppingToRect(cropRect)
}
}
func colorOverlay(color: UIColor) -> Filter {
return { image in
let overlay = self.colorGenerator(color)(image)
return self.compositeSourceOver(overlay)(image)
}
}
func composeFilters(filter1: Filter, _ filter2: Filter) -> Filter {
return { image in
filter2(filter1(image))
}
}
}
//自定义运算符
infix operator +++ { associativity left }
func +++ (filter1: Filter, filter2: Filter) -> Filter {
return { image in filter2(filter1(image)) }
}
extension CIImage {
//扩展下方便点语法使用
func filters(myFilter: Filter) -> CIImage {
return myFilter(self)
}
}
使用ViewController.swift
import UIKit
class SecondViewController: UIViewController {
@IBOutlet var colorButtonCollection: [UIButton]! {
didSet {
colorButtonCollection.forEach {
$0.layer.cornerRadius = $0.frame.width/2
}
}
}
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var blurSlider: UISlider!
lazy var originalImage:UIImage = {
return UIImage(named: "beauty2")
}()!
lazy var overlayColor:UIColor = {
return UIColor.purpleColor().colorWithAlphaComponent(0.2)
}()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sliderClick(sender: UISlider) {
let inputImage = CIImage(image: originalImage)!
let Filter = BFilter()
let myFilter = Filter.blur(Double(sender.value)) +++ Filter.colorOverlay(overlayColor)
let outputImage = inputImage.filters(myFilter)
imageView.image = UIImage(CIImage: outputImage)
}
@IBAction func colorButtonClick(sender: UIButton) {
let btnText = sender.titleLabel?.text
var selectColor:UIColor!
switch btnText! {
case "黑":
selectColor = UIColor.redColor()
case "橙":
selectColor = UIColor.orangeColor()
case "紫":
selectColor = UIColor.purpleColor()
case "红":
selectColor = UIColor.redColor()
case "蓝":
selectColor = UIColor.blueColor()
case "绿":
selectColor = UIColor.greenColor()
default:
break
}
overlayColor = selectColor.colorWithAlphaComponent(0.2)
sliderClick(blurSlider)
}
}