Xcode9學習筆記55 - 使用CoreImage框架設置圖片的單色效果

import UIKit
//導入CoreImage框架,該框架提供了強大和高效的圖像處理功能,用來對基於像素的圖像進行分析、操作和特效處理
import CoreImage

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let image = UIImage(named: "Pic1")
        let imageView = UIImageView(image: image)
        self.view.addSubview(imageView)
        
        let ciImage = CIImage(image: image!)//初始化一個圖像對象,並加載之前導入的圖片
        //初始化一個顏色對象,並設置其顏色值爲棕色,其參數值介於0和1之間
        let color = CIColor(red: 0.8, green: 0.6, blue: 0.4)
        let filter = CIFilter(name: "CIColorMonochrome")//初始化一個濾鏡對象,並設置濾鏡類型爲單色調濾鏡
        filter?.setValue(color, forKey: kCIInputColorKey)//設置單色調濾鏡的輸入顏色值
        filter?.setValue(1.0, forKey: kCIInputIntensityKey)//設置單色調濾鏡的顏色濃度值
        filter?.setValue(ciImage, forKey: kCIInputImageKey)//設置需要應用單色調濾鏡的圖像
        let outImage = filter?.outputImage//獲得應用單色調濾鏡後的圖像
        
        imageView.image = UIImage(ciImage: outImage!)//ciImage,前2個字符要小寫
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}



發佈了95 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章