CoreImage 小试牛刀,这里只有🐟,没有什么渔
CIImage+Ex.swift
import UIKit
// MARK: - 单色滤镜 - 一些事例
extension CIImage {
enum PhotoEffect:String {
case instant = "CIPhotoEffectInstant"
case noir = "CIPhotoEffectNoir"
case tonal = "CIPhotoEffectTonal"
case transfer = "CIPhotoEffectTransfer"
case mono = "CIPhotoEffectMono"
case fade = "CIPhotoEffectFade"
case process = "CIPhotoEffectProcess"
case chrome = "CIPhotoEffectChrome"
}
/*
* 改变照片效果
*/
func changed(photoEffect: PhotoEffect) -> CIImage {
guard let filter = CIFilter(name: photoEffect.rawValue) else {
fatalError()
}
filter.setValue(self, forKey: kCIInputImageKey)
guard let outputImage = filter.outputImage else {
fatalError()
}
return outputImage
}
}
// MARK: - 修改色调
extension CIImage {
func hueAdjust(value: Float) -> CIImage {
//该属性类型:角度 最大值:Ԉ 最小值:-Ԉ
var value = value
value = value > Float(M_PI) ? Float(M_PI) : value
value = value < Float(-M_PI) ? Float(-M_PI) : value
guard let filter = CIFilter(name: "CIHueAdjust") else {
fatalError()
}
filter.setValue(value, forKey: kCIInputAngleKey)
filter.setValue(self, forKey: kCIInputImageKey)
guard let outputImage = filter.outputImage else {
fatalError()
}
return outputImage
}
}
就不上传到github了,没啥内容
使用例子:
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet var papaButtonCollection: [UIButton]! {
didSet {
papaButtonCollection.forEach {
$0.layer.cornerRadius = 5
$0.layer.borderWidth = 0.5
$0.layer.borderColor = UIColor.grayColor().CGColor
}
}
}
@IBOutlet weak var imageView: UIImageView!
lazy var originalImage: UIImage = {
return UIImage(named: "beauty2")
}()!
}
extension ViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//全部button连接这里
@IBAction func papaButtonClick(sender: UIButton) {
let btnText = sender.titleLabel?.text
var inputImage = CIImage(image: originalImage)!
switch btnText! {
case "怀旧":
inputImage = inputImage.changed(.instant)
case "黑白":
inputImage = inputImage.changed(.noir)
case "灰白":
inputImage = inputImage.changed(.tonal)
case "岁月":
inputImage = inputImage.changed(.transfer)
case "单色":
inputImage = inputImage.changed(.mono)
case "褪色":
inputImage = inputImage.changed(.fade)
case "冲印":
inputImage = inputImage.changed(.process)
case "铬黄":
inputImage = inputImage.changed(.chrome)
default:
break
}
imageView.image = UIImage(CIImage: inputImage)
}
@IBAction func sliderClick(sender: UISlider) {
let inputImage = CIImage(image: originalImage)!
let currImg = inputImage.hueAdjust(sender.value)
imageView.image = UIImage(CIImage: currImg)
}
}