我在“我的照片编辑”应用中使用了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
}
答案 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的输出: