Skip to content

Instantly share code, notes, and snippets.

@katiesmillie
Created January 18, 2017 22:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save katiesmillie/45bbcc352c7f728093c81de21d17b921 to your computer and use it in GitHub Desktop.
Save katiesmillie/45bbcc352c7f728093c81de21d17b921 to your computer and use it in GitHub Desktop.
func imageByAddingBorder(width: CGFloat, color: UIColor) -> UIImage? {
UIGraphicsBeginImageContext(self.size)
let imageRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
self.draw(in: imageRect)
let context = UIGraphicsGetCurrentContext()
let borderRect = imageRect.insetBy(dx: width / 2, dy: width / 2)
context?.setStrokeColor(color.cgColor)
context?.setLineWidth(width)
context?.stroke(borderRect)
let borderedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return borderedImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment