Swift图像处理之优化照片

时间:2021-11-24 14:08:42

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.效果图如下(右侧为优化后的):

Swift图像处理之优化照片

4.代码如下(所有滤镜都使用):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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暂时无法解决,待解决了我再补充,大家自己用起来就知道在哪里了)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
      })
  }
}

此段代码运行效果如下:

Swift图像处理之优化照片