Swift影像處理之最佳化照片_Swift

來源:互聯網
上載者:User

Core Image能通過分析圖片的各個屬性,人臉的地區等進行自動最佳化圖片。我們只需要調用autoAdjustmentFiltersWithOptions這個API方法擷取各個自動增強濾鏡來最佳化圖片即可。不管是人物照片還是風景照均可增強效果。

(以前另外還有個叫autoAdjustmentFilters的方法,現已廢除。)

1.具體使用的濾鏡如下:

(1)CIRedEyeCorrection:修複因相機的閃光燈導致的各種紅眼
(2)CIFaceBalance:調整膚色
(3)CIVibrance:在不影響膚色的情況下,改善映像的飽和度
(4)CIToneCurve:改善映像的對比
(5)CIHighlightShadowAdjust:改善陰影細節

2.autoAdjustmentFiltersWithOptions方法參數說明(字典類型):

(1)CIDetectorImageOrientation提供映像方向:使Core Image能更精確的定位到臉的位置,對CIRedEyeCorrection和CIFaceBalance濾鏡很有用。
(2)kCIImageAutoAdjustEnhance設為false :只需要消除紅眼,不要其他濾鏡
(3)kCIImageAutoAdjustRedEye設為false :消除紅眼不要,其他濾鏡都要

3.效果圖如下(右側為最佳化後的):

4.代碼如下(所有濾鏡都使用):

import UIKitclass ViewController: UIViewController{  @IBOutlet weak var imageView: UIImageView!  //圖片原圖  lazy var originalImage: UIImage = {    return UIImage(named: "IMG_0473.jpg")  }()!  lazy var context: CIContext = {    return CIContext(options: nil)    }()  override func viewDidLoad() {    super.viewDidLoad()  }  override func didReceiveMemoryWarning() {    super.didReceiveMemoryWarning()  }  //最佳化圖片  @IBAction func autoAdjustImage(sender: AnyObject) {    var inputImage = CIImage(image: originalImage)    let options:[String : AnyObject] = [CIDetectorImageOrientation:1] //圖片方向    let filters = inputImage!.autoAdjustmentFiltersWithOptions(options)    //遍曆所有濾鏡,依次處理映像    for filter: CIFilter in filters {      filter.setValue(inputImage, forKey: kCIInputImageKey)      inputImage = filter.outputImage    }    let cgImage = context.createCGImage(inputImage!, fromRect: inputImage!.extent)    self.imageView.image = UIImage(CGImage: cgImage)  }  //還原圖片  @IBAction func resetImage(sender: AnyObject) {     self.imageView.image = originalImage  }}

研究並自己敲一敲或者做成直接用的類是不錯的選擇.下面的地址不是寫死的,加了拍照最佳化的功能,代碼如下:(但是有一個BUG暫時無法解決,待解決了我再補充,大家自己用起來就知道在哪裡了)

import UIKit//從相簿選取或者拍照需要實現UIImagePickerControllerDelegate UINavigationControllerDelegateclass ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate{  @IBOutlet weak var imageView: UIImageView!  var chooseImage: UIImage = UIImage()  //圖片原圖//  lazy var originalImage: UIImage = UIImage(named: "jobZ.png")!//  //  lazy var context: CIContext = CIContext(options: nil)  lazy var context: CIContext = {    return CIContext(options: nil)  }()  override func viewDidLoad() {//    self.imageView.image = UIImage(named: "jobZ.png")  }  //美化圖片  @IBAction func autoAdjustImage(sender: UIButton) {    var inputImage = CIImage(image: self.chooseImage)    let options:[String : AnyObject] = [CIDetectorImageOrientation:1] //圖片方向    let filters = inputImage!.autoAdjustmentFiltersWithOptions(options)    //遍曆所有濾鏡,依次處理映像    for filter: CIFilter in filters {      filter.setValue(inputImage, forKey: kCIInputImageKey)      inputImage = filter.outputImage    }    let cgImage = context.createCGImage(inputImage!, fromRect: inputImage!.extent)    self.imageView.image = UIImage(CGImage: cgImage)  }  //選取圖片  @IBAction func chooseImage(sender: UIButton) {    if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){      //初始化圖片控制器      let picker = UIImagePickerController()      //設定代理      picker.delegate = self      //指定圖片控制器類型 source我這裡寫的是用拍照,如果想用圖庫後面值改成UIImagePickerControllerSourceType.PhotoLibrary即可      picker.sourceType = UIImagePickerControllerSourceType.Camera      //如果有自拍則調用自拍      if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front){        picker.cameraDevice = UIImagePickerControllerCameraDevice.Front      }      //彈出控制器,顯示介面      self.presentViewController(picker, animated: true, completion: {        () -> Void in      })    }else{      print("讀取相簿錯誤")    }  }  //還原圖片  @IBAction func resetImage(sender: UIButton) {    self.imageView.image = self.chooseImage  }  //代理  //選擇圖片成功後代理  func imagePickerController(picker: UIImagePickerController,    didFinishPickingMediaWithInfo info: [String : AnyObject]) {      //查看info對象      print(info)      //擷取選擇的原圖      let image = info[UIImagePickerControllerOriginalImage] as! UIImage      self.imageView.image = image      self.chooseImage = image      //圖片控制器退出      picker.dismissViewControllerAnimated(true, completion: {        () -> Void in      })  }}

此段代碼運行效果如下:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.