I have a UIBezierPath
which crops part of an UIImage
and then I want to apply feather/fade effect starting from bezierpath towards inside in Objective-C but I dont want to use any gradient color or shadow or any tricks like that. I want the actual image’s alpha in its borders, go from 100% to 0%. I’ve searched a lot but non of the solutions like this one:
- iOS: Feather with glow/shadow effect on uiimage
- iOS and Android Algorithm or library for feathering edges of the images similar to photoshop's
has worked for me yet. The result that I want is similar to the image below. How can I achieve this? Any help is much appreciated.
3
Answers
So here is the Objective-c version I used which worked much better than I expected. There is an important thing to consider. In my personal experience, I got the best Fade/Feather effect when the value for @inputRadius was between 6-8, even though the documentation says it can be between 1-10, you can basically set higher values than 10 too but if you want to get the best Edge feathering result, 6-8 is the best. Here's my code.
You actually don’t need a gradient for this you can use a dropShadow with a white color to achieve this effect. You will need two separate views. One view should have its layer.mask set to a CAShapeLayer equal to the UIBezierPath.cgPath. You have to also turn on maskToBounds for this to work. Unfortunately maskToBounds will prevent the shadow from drawing on this layer because the shadow is outside of the bounds, which is why you need a second view. It should have a layer = a CAShapeLayer with the same path, and a dropShadow applied with white.
Here is playground showing how to do it. I felt the shadow blur was too weak so I used Core Image’s gaussian blur instead. I thought core image is supposed to be faster these days, but it still seems pretty bad; not sure if its the play ground or not. Try GPU Image if it needs to be faster.