程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> Swift圖象處置之優化照片

Swift圖象處置之優化照片

編輯:更多關於編程

Swift圖象處置之優化照片。本站提示廣大學習愛好者:(Swift圖象處置之優化照片)文章只能為提供參考,不一定能成為您想要的結果。以下是Swift圖象處置之優化照片正文


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 UIKit
class 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 UINavigationControllerDelegate
class 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
      })
  }
}

此段代碼運轉後果以下:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved