使用滑块的模糊效果无法正常工作
我使用了一个滑块来模糊我的图像效果。 我使用以下代码:
let currentFilter = CIFilter(name: "CIGaussianBlur") let beginImage = CIImage(image: imgImage.image!) currentFilter!.setValue(beginImage, forKey: kCIInputImageKey) currentFilter!.setValue(sender.value, forKey: kCIInputRadiusKey) let cropFilter = CIFilter(name: "CICrop") cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey) cropFilter!.setValue(CIVector(cgRect: beginImage!.extent), forKey: "inputRectangle") let output = cropFilter!.outputImage let cgimg = context.createCGImage(output!, from: output!.extent) let processedImage = UIImage(cgImage: cgimg!) imgImage.image = processedImage
问题是,当滑块的值递增时,模糊效果会出现,但是当我减小滑块的值时,它将不会消除模糊效果。
您可以将滤镜应用于原始图像,而不是已过滤的图像。 试试下面的代码。
let context = CIContext(options: nil) @IBAction func blurSlider(_ sender: UISlider) { let originalImage = UIImage(named: "Your Image Name") let currentValue = Int(sender.value) let currentFilter = CIFilter(name: "CIGaussianBlur") currentFilter!.setValue(CIImage(image: originalImage!), forKey: kCIInputImageKey) currentFilter!.setValue(currentValue, forKey: kCIInputRadiusKey) let cropFilter = CIFilter(name: "CICrop") cropFilter!.setValue(currentFilter!.outputImage, forKey: kCIInputImageKey) cropFilter!.setValue(CIVector(cgRect: (CIImage(image: originalImage!)?.extent)!), forKey: "inputRectangle") let output = cropFilter!.outputImage let cgimg = context.createCGImage(output!, from: output!.extent) let processedImage = UIImage(cgImage: cgimg!) imgImage.image = processedImage }
模拟器输出:
注意:我建议您在真实设备中测试代码,因为coreImage上的模拟器性能太慢。
创建单独的类,添加模糊效果:
import UIKit import GLKit class BlurImageView: GLKView { let clampFilter = CIFilter(name: "CIAffineClamp")! let blurFilter = CIFilter(name: "CIGaussianBlur")! let ciContext:CIContext override init(frame: CGRect) { let glContext = EAGLContext(API: .OpenGLES2) ciContext = CIContext( EAGLContext: glContext, options: [ kCIContextWorkingColorSpace: NSNull() ] ) super.init(frame: frame, context: glContext) enableSetNeedsDisplay = true } required init(coder aDecoder: NSCoder) { let glContext = EAGLContext(API: .OpenGLES2) ciContext = CIContext( EAGLContext: glContext, options: [ kCIContextWorkingColorSpace: NSNull() ] ) super.init(coder: aDecoder) context = glContext enableSetNeedsDisplay = true } @IBInspectable var inputImage: UIImage? { didSet { inputCIImage = inputImage.map { CIImage(image: $0)! } } } @IBInspectable var blurRadius: Float = 0 { didSet { blurFilter.setValue(blurRadius, forKey: "inputRadius") setNeedsDisplay() } } var inputCIImage: CIImage? { didSet { setNeedsDisplay() } } override func drawRect(rect: CGRect) { if let inputCIImage = inputCIImage { clampFilter.setValue(inputCIImage, forKey: kCIInputImageKey) blurFilter.setValue(clampFilter.outputImage!, forKey: kCIInputImageKey) let rect = CGRect(x: 0, y: 0, width: drawableWidth, height: drawableHeight) ciContext.drawImage(blurFilter.outputImage!, inRect: rect, fromRect: inputCIImage.extent()) } } }
如何使用 :
class ViewController: UIViewController { @IBOutlet var imageView: BlurImageView! override func viewDidLoad() { super.viewDidLoad() imageView.inputImage = UIImage(named:"testImage")! } @IBAction func sliderValueChanged(sender: UISlider) { imageView.blurRadius = sender.value } }