How to blur an image using CIFilter with Swift 4 — Programming with Swift

Darren
Programming with Swift
2 min readSep 2, 2018

Checkout my other posts over at programmingwithswift.com

This is going to be a quick tutorial to show you how to easily blur images using CIFilter in Swift 4.

In the above image I have create a function that will blur a UIImage. The function works by taking two arguments. The first argument will be the UIImage that you want to blur and the second argument will be the blur radius amount.

Inside the function the first thing that I do is I convert the UIImage into a CIImage so that we can use CIFilter on it.

Next I create the filter by initialising a new filter with the filter type that I want. In this case I use CIGaussianBlur.

After the filter has been initialised, I set the image that the filter needs to be applied to. This happens on the line just below I initialise the filter. After I have told the filter what image to use, I set the blur radius that I want. This will use the second argument that the function takes.

Once all of this is done, I get the output image from the blurFilter and then I create a new UIImage from the output image an return the new UIImage at the end of the function.

The above image shows you how to use the function. I create a new image variable that will store the return value from the blurImage function.

After that I create a new UIImageView and set the image to be the blurred image that was returned above.

I create a frame for the image view, set the content mode and then just add it as a subview to the view controllers view, and that is pretty much it.

Other tutorials:

How to use SKStoreReviewController tutorial: https://medium.com/programming-with-swift/skstorereviewcontroller-tutorial-programming-with-swift-31a4faded01f

How to read a Barcode or QRCode with Swift: https://medium.com/programming-with-swift/how-to-read-a-barcode-or-qrcode-with-swift-programming-with-swift-10d4315141d2

--

--