The optimized photo _swift for Swift image processing

Source: Internet
Author: User
Tags uikit

Core image can automatically optimize the image by analyzing the properties of the image and the area of the face. We just need to call Autoadjustmentfilterswithoptions This API method to get each automatic enhancement filter to optimize the picture. Whether it's a person's photo or a landscape picture, it can enhance the effect.

(There was a previous method called Autoadjustmentfilters, which has been abolished.) )

1. The specific use of the filter is as follows:

(1) Ciredeyecorrection: Fix the various red eye caused by the camera's flash
(2) Cifacebalance: Adjust skin tone
(3) Civibrance: Improve the saturation of the image without affecting the complexion
(4) Citonecurve: Improve the contrast of the image
(5) Cihighlightshadowadjust: Improve shadow Detail

2.autoAdjustmentFiltersWithOptions Method parameter Description (dictionary type):

(1) cidetectorimageorientation provides image orientation: Enables the core image to be positioned more precisely to the position of the face, which is useful for ciredeyecorrection and cifacebalance filters.
(2) Kciimageautoadjustenhance set to false: only need to remove red eye, no other filters
(3) Kciimageautoadjustredeye set to false: Remove red eye, other filters are

3. The effect chart is as follows (the right is optimized):

4. The code is as follows (all filters are used):

Import Uikit class viewcontroller:uiviewcontroller{@IBOutlet weak var imageview:uiimageview!
  Image lazy var originalimage:uiimage = {return UIImage (named: "Img_0473.jpg")} ()! Lazy var context:cicontext = {return Cicontext (Options:nil)} () override Func Viewdidload () {super.viewd  Idload ()} override func didreceivememorywarning () {super.didreceivememorywarning ()}//optimize picture @IBAction func Autoadjustimage (sender:anyobject) {var inputimage = ciimage (image:originalimage) Let Options:[string:anyobje CT] = [cidetectorimageorientation:1]//Picture direction let filters = inputimage!. Autoadjustmentfilterswithoptions (options)//traverse all filters, sequentially process the image 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)}//Restore picture @IBAction func resetimage (sender:anyobject) {self.imageView.image = originalimage}} 

It is a good choice to study and knock yourself out or make a direct use of the class. The following address is not written dead, add a photo optimization function, the code is as follows: (but there is a bug can not be solved, I will be resolved to add, we use it to know where)

Import Uikit///from album selection or photo need to implement Uiimagepickercontrollerdelegate Uinavigationcontrollerdelegate class Viewcontroller: Uiviewcontroller, uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate{@IBOutlet weak var ImageView:
  uiimageview!
var chooseimage:uiimage = UIImage ()//Picture original//lazy var originalimage:uiimage = uiimage (named: "Jobz.png")! Lazy var context:cicontext = Cicontext (options:nil) lazy var context:cicontext = {return Cicontext (Opti Ons:nil)} () override Func Viewdidload () {//Self.imageView.image = UIImage (named: "Jobz.png")}//Landscaping picture @IB Action func autoadjustimage (Sender:uibutton) {var inputimage = ciimage (image:self.chooseImage) Let Options:[str Ing:anyobject] = [Cidetectorimageorientation:1]//Picture direction let filters = inputimage!. Autoadjustmentfilterswithoptions (options)//traverse all filters, sequentially process the image 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)}//Select Picture @IBAction func chooseimage (Sender:uibutton) { If Uiimagepickercontroller.issourcetypeavailable (.
      Photolibrary) {///Initialize picture controller Let Picker = Uiimagepickercontroller ()//set proxy picker.delegate = self Specify picture controller type source I write here is to use a photo, if you want to use the gallery after the face value to Uiimagepickercontrollersourcetype.photolibrary can be picker.sourcetype = Uiima Gepickercontrollersourcetype.camera//If there is a front-facing camera, call the front camera if uiimagepickercontroller.iscameradeviceavailable (UIIm
      Agepickercontrollercameradevice.front) {picker.cameradevice = Uiimagepickercontrollercameradevice.front}
    Eject controller, display interface Self.presentviewcontroller (picker, animated:true, completion: {()-> Void in}) }else{Print ("read album error")}//Restore Picture @IBAction func resetimage (Sender:uibutton) {Self.imageView.ima GE = Self.chooseimage}//Agent//Select image after successful agent Func Imagepickercontroller (Picker:uiimagepickercontroller, Didfinishpick Ingmediawithinfo info: [String:anyobject]) {//View Info object Print (info)//Get selected artwork let image = Info[u Iimagepickercontrolleroriginalimage] As! UIImage Self.imageView.image = Image Self.chooseimage = image//Picture controller exit Picker.dismissviewcontroll Eranimated (True, completion: {()-> Void in})}}

This code works as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.