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

**URL:** <https://forums.kodeco.com/t/detect-border-edges-of-a-uiimage-with-white-color-background-and-crop-it-in-swift/22374>\
**Category:** Swift\
**Created:** [November 8, 2016, 6:40am UTC](https://forums.kodeco.com/t/detect-border-edges-of-a-uiimage-with-white-color-background-and-crop-it-in-swift/22374 "2016-11-08T06:40:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecoapps](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ecoapps/32/45354_2.png) [@ecoapps](https://forums.kodeco.com/u/ecoapps)\
**Post date:** [November 8, 2016, 6:40am UTC](https://forums.kodeco.com/t/detect-border-edges-of-a-uiimage-with-white-color-background-and-crop-it-in-swift/22374/1 "2016-11-08T06:40:56Z")

</div>

Reposting from StackOverFlow:  
[Detect border edges of a UIImage with white color background and crop it in Swift - Stack Overflow](http://stackoverflow.com/questions/40480246/detect-border-edges-of-a-uiimage-with-white-color-background-and-crop-it-in-swif)

I have a UIImage [with a background color](https://i.stack.imgur.com/NCr7c.png)  
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…
