Detect border edges of a UIImage with white color background and crop it in Swift

Reposting from StackOverFlow:

I have a UIImage with a background color
It is shown in Orange color for visibility here but in reality it is white color for me.
How can I detect the edges of the image and crop it to it’s shape?
There are solutions posted in Objective-C but I am working in Swift 2.1.1
Also, the previous solutions have some limitations and are not complete like they don’t work for white backgrounds
Can someone please post the code for such complex UIImage cropping in Swift…