Adding a blur effect to a custom camera session in iOS (Swift 3)
May 7, 2017
In this blog post we will look at creating a custom camera session and adding a blur effect over it. This effect can be seen in apps like Snapchat while shipping down to show the user profile view.
With iOS 10 and Swift 3, Apple has simplified the process of creating your custom camera implementation. We begin by creating reference to our AVCaptureSession.
In our viewDidLoad() or viewWillAppear() we begin the setup of our session, and capture device.
Now we setup our preview layer which will show the preview from our camera.
Once the preview layer is setup calling session.startRunning() should start the camera and display the output from the camera on our view.
Once we have our camera setup, adding the blur view is quite simple. UIKit provides a visual effects view API that allows us to add some interesting effects to our views.
In the code above we start by creating a blur effect with a dark style and then instantiate a visual effects view with the blur effect. Finally we add the visual effects view to our main view. The results are pretty awesome.
Adding a blur effect to a realtime camera output is quite easy and can really enhance the user experience. This effect can be used as a background view, you can add a pan gesture recognizer and change the alpha values of the blur view to control the blur effect (like how snapchat does). The possibilities are endless.