动画iOS进阶指南iOS 开发

iOS开发 - CoreImage 小试牛刀

2016-09-07  本文已影响228人  小黑Swift
CoreImage 小试牛刀,这里只有🐟,没有什么渔
Simulator Screen Shot 2016年9月7日 下午5.18.19.png

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了,没啥内容

使用例子:

SB快速布局

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)
    }
}

参考(渔):iOS8 Core Image In Swift
上一篇下一篇

猜你喜欢

热点阅读