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 }) }}
此段代码运行效果如下: