Pencil Sketch Filter in iOS App Using Swift

Vojta Struhár
2 min readOct 2, 2019

There is no simple way to create 'Pencil sketch' filter for iOS. Common answer to this problem actually is to write your own algorithm. But aint nobody got time for that 😤

Luckily for us, there is another way, using iOS native Core Image Filters.

Final product of this tutorial

Before reading this tutorial, I highly recommend reading Jared's post. He does excellent job at making applying CIFilters easy. His post helped me a lot and I will be referencing it 💛

If you read Jared's article, you know how to apply CIFilters. Congratulations, we're almost there.

The answer to our problem is which ones work the best, or more specifically which filter combo is the best. I found this to be pretty good looking:

  1. Posterize
  2. Noise reduction (used to make more noise in the image rather than to reduce it)
  3. Sharpen the edges
  4. Black and gray

Here are filters that I played with to get this result. Some could be useful to you, so feel free to play with them or even try new ones — you can find them in Apple Documentation.

The application process, thanks to Jared, is really simple.

let sketch = originalImage.addFilter(filterType: .Posterize).addFilter(filterType: .NoiseReduction).addFilter(filterType: .Sharpen).addFilter(filterType: .MonoChrome)

Here is my method for adding CIFilters. You can tweak the values for the best result, however I was satisfied with this.

I hope this post helped you! As I said — feel free to play with various filter input values or even combination of the filters.

Happy coding! 🖥

--

--

Vojta Struhár

Always on the lookout for a smarter way to code. Mac enjoyer 🍎, Web developer 🕸️, Game developer 👾