Ios 如何在swift 3中启用图像过滤器?

Ios 如何在swift 3中启用图像过滤器?,ios,swift3,uiimageview,imagefilter,Ios,Swift3,Uiimageview,Imagefilter,我有一个包含图像的图像视图,我想将过滤器添加到我使用的过滤器代码的图像中,然后将新图像替换为旧图像这是我的代码: let filterNames = CIFilter.filterNames(inCategories: nil) @IBOutlet weak var myImage: UIImageView! @IBAction func filter1(_ sender: Any) { func simpleBlurFilterExample(myImage: UIImage)

我有一个包含图像的图像视图,我想将过滤器添加到我使用的过滤器代码的图像中,然后将新图像替换为旧图像这是我的代码:

let filterNames = CIFilter.filterNames(inCategories: nil)

@IBOutlet weak var myImage: UIImageView!

@IBAction func filter1(_ sender: Any) {

    func simpleBlurFilterExample(myImage: UIImage) -> UIImage {
        // convert UIImage to CIImage
        let inputCIImage = CIImage(image: myImage)!

        // Create Blur CIFilter, and set the input image
        let blurFilter = CIFilter(name: "CIGaussianBlur")!
        blurFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
        blurFilter.setValue(8, forKey: kCIInputRadiusKey)

        // Get the filtered output image and return it
        let myImage = blurFilter.outputImage!
        return UIImage(ciImage: myImage)


    }



}

正如您在这里看到的,我有一个名为“我的图像”的输出图像,我希望输入图像成为我的图像,按下按钮后,使用过滤器将图像更改为输出图像

,您可以使用脚本或编程方式为UIImageView设置初始图像,也可以是viewDidLoad
myImage.image=UIImage(名为:“yourImage”)
。然后使用参数
myImage.image
调用
simpleBlurFilterExample
函数并使用返回值

        @IBAction func filter1(_ sender: Any) {
               myImage.image = simpleBlurFilterExample(myImage: myImage.image)
        }

        func simpleBlurFilterExample(myImage: UIImage) -> UIImage {
                // convert UIImage to CIImage
                let inputCIImage = CIImage(image: myImage)!

                // Create Blur CIFilter, and set the input image
                let blurFilter = CIFilter(name: "CIGaussianBlur")!
                blurFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
                blurFilter.setValue(8, forKey: kCIInputRadiusKey)

                // Get the filtered output image and return it
                let myImage = blurFilter.outputImage!
                return UIImage(ciImage: myImage)
}

您需要在UIImageView中设置图像

myImage.image = filter1(myImage: myImage.image)

也许您应该调用imageView myImageView以减少混淆

您需要修改代码并使用正确的CIimage方法和正确的选项

@IBAction func filter1(_ sender: Any) {
           myImage.image = simpleBlurFilterExample(myImage: myImage.image)
    }

    func simpleBlurFilterExample(myImage: UIImage) -> UIImage {
            // convert UIImage to CIImage
            let inputCIImage = CIImage(cgImage: myImage.cgImage!)

            // Create Blur CIFilter, and set the input image
            let blurFilter = CIFilter(name: "CIGaussianBlur")
            blurFilter?.setValue(inputCIImage, forKey: kCIInputImageKey)
            blurFilter?.setValue(8, forKey: kCIInputRadiusKey)

            if let output = blurFilter?.outputImage {

              return UIImage(ciImage: output)

            }else{
              return myImage
            }

}

那么问题在哪里呢。这个代码不起作用吗?错误是什么?完全不工作没有错误您是否正在将此函数返回的图像设置为imageview?正在工作,但我将收到错误致命错误:在展开可选值(lldb)时意外发现nil。应用程序将因此行而崩溃返回UIImage(cgImage:输出为!cgImage)线程1:EXC_BAD_指令(代码=EXC_I386_INVOP,子代码=0x0)编辑了它。现在检查,应该可以工作了。我尝试了我的设置,它成功了。使用CIimage作为CGimage是一个错误。谢谢,它可以工作,但需要很多秒才能完成效果。它如何工作得更快?我只需在故事板中显示图像视图,并在上面添加一个按钮即可。这可以工作,但当用户想再次使用过滤器时,应用程序将崩溃。应用程序将如何崩溃?在我使用代码之前一切正常myImage.image=UIImage(名为:“2.jpg”)再次进行筛选非常感谢请投票表决我的问题