使用滑块的模糊效果无法正常工作

时间:2017-09-08 04:27:40

标签: ios swift slider blur

我使用了一个滑块来模糊效果到我的图像。我使用以下代码:

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

问题是每当滑块的值递增时,模糊效果会出现,但是当我减小滑块的值时,它就不会消除模糊效果。

2 个答案:

答案 0 :(得分:2)

您可以将滤镜应用于原始图像,而不是已过滤的图像。请尝试以下代码。

    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

    }

模拟器的输出:

enter image description here

注意:我建议您在真实设备中测试代码,因为CoreImage上的模拟器性能太慢。

答案 1 :(得分:1)

创建单独的类,添加模糊效果:

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
}
}