Saeed Rahmatolahi
Saeed Rahmatolahi

Reputation: 1338

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

Upvotes: 0

Views: 1371

Answers (3)

Kapil G
Kapil G

Reputation: 4141

You need to modify your code and use right methods of CIimage with correct optionals

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

}

Upvotes: 0

adamfowlerphoto
adamfowlerphoto

Reputation: 2751

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

Upvotes: 1

a.u.b
a.u.b

Reputation: 1609

You can set initial image for your UIImageView with Storyboard or programmatically, maybe viewDidLoad myImage.image = UIImage(named: "yourImage"). Then call simpleBlurFilterExample function with parameter myImage.image and use return value.

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

Upvotes: 0

Related Questions