Search code examples
iosswift3uiimageviewimagefilter

how to enable filter for Image in swift 3?


I have an Image View that contains image and I want to add filter to the image I used codes for filter and then put new image instead of old image here is my code:

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)


    }



}

as you see here i have an outlet image that named my image and I want input image to be my image and after pushing button change image to the output image with filter


Solution

  • You need to set the image in the UIImageView

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

    Maybe you should call the imageView myImageView to reduce confusion as well