iOS UIImage Resizable image with caps


In the example of a message bubble illustrated below: the corners of the image should remain unchanged which is specified by UIEdgeInsets, but the borders and center of the image should expand to cover the new size.

original image

resized image

 let insets = UIEdgeInsetsMake(12.0, 20.0, 22.0, 12.0)
 let image = UIImage(named: "test")
 image?.resizableImageWithCapInsets(insets, resizingMode: .Stretch)