简体   繁体   中英

CIFilter applied to an image not working - swift

I am trying to apply a CIFilter to an image that is already displayed in my imageView in my single view application but it does not appear to have applied the filter. Please can someone advise? My input image appear exactly the same.

SWIFT:

let filter = CIFilter.init(name: "CIHueAdjust")
let context = CIContext()
var extent: CGRect!
var scaleFactor: CGFloat!

@IBOutlet weak var img: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    let ciImage = CIImage.init(image: img.image!)

    filter?.setDefaults()
    filter?.setValue(ciImage, forKeyPath: kCIInputImageKey)
    let result = filter?.outputImage
    print("result: \(result)")

    var image = UIImage.init(cgImage: context.createCGImage(result!, from: result!.extent)!)
    img.image = image


    img.image = image
}

CONSOLE:

 result: Optional(<CIImage: 0x1c001a7a0 extent [0 0 2016 1512]>
 affine [1 0 0 -1 0 1512] extent=[0 0 2016 1512] opaque
colormatch "sRGB IEC61966-2.1"_to_workingspace extent=[0 0 2016 1512] opaque
  IOSurface 0x1c401a780(169) seed:1 YCC420f 601 alpha_one extent=[0 0 2016 1512] opaque

It's in the comments, but here's the full (and better formatted) answer on how to set up a call to CIHueAdjust , using Swift 4:

let filter = CIFilter(name: "CIHueAdjust")
let context = CIContext()
var extent: CGRect!
var scaleFactor: CGFloat!

@IBOutlet weak var img: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    let ciImage = CIImage(image: img.image!)

    // Note: you may use kCIInputImageKey for inputImage
    filter?.setValue(ciImage, forKey: "inputImage")
    filter?.setValue(Float(1), forKey: "inputAngle")
    let result = filter?.outputImage

    var image = UIImage(cgImage: context.createCGImage(result!, from: result!.extent)!)
    img.image = image

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM