无法使用UISlider降低CIVignette效果

时间:2017-10-10 06:40:55

标签: ios swift3 core-image cifilter

我在“我的照片编辑”应用中使用了CIVignette效果。它基于Slider的Changing事件应用。我使用了以下代码,我的问题是每当我增加滑块的值时会出现Vignette效果,但每当我减小滑块的值时,Vignette效果都不会降低。
请帮助。

@IBAction func slider(_ sender: UISlider) {

        let startImage = CIImage(image: imgEdited!)!

        let vignetteFilter = CIFilter(name: "CIVignette")!

        let radius = 5

        vignetteFilter.setValue(startImage, forKey:kCIInputImageKey)
        vignetteFilter.setValue(sender.value, forKey:"inputIntensity")
        vignetteFilter.setValue(radius, forKey:"inputRadius")

        let filteredImageData = vignetteFilter.value(forKey: kCIOutputImageKey) as! CIImage
        let filteredImageRef = self.context.createCGImage(filteredImageData, from: filteredImageData.extent)

        imgImage.image = UIImage.init(cgImage: filteredImageRef!)
        self.imgEdited = imgImage.image
}

1 个答案:

答案 0 :(得分:2)

根据我对其他SO帖子Blur effect using slider Is not working properly的回答,代码如下。

 @IBAction func slider(_ sender: UISlider) {

    let startImage = CIImage(image: UIImage(named: "Your Image Name")!) // You should initialise your UIImage here not UIImageView
    let vignetteFilter = CIFilter(name: "CIVignette")! 

    vignetteFilter.setValue(startImage, forKey:kCIInputImageKey)
    vignetteFilter.setValue(sender.value, forKey:kCIInputIntensityKey)
    vignetteFilter.setValue(5, forKey:kCIInputRadiusKey)

    let filteredImageData = vignetteFilter.value(forKey: kCIOutputImageKey) as! CIImage
    let filteredImageRef = self.context.createCGImage(filteredImageData, from: filteredImageData.extent)

    self.imgView.image = UIImage.init(cgImage: filteredImageRef!) // Output

 }

Sim的输出:

enter image description here