Created
January 18, 2017 22:05
-
-
Save katiesmillie/45bbcc352c7f728093c81de21d17b921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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