Forked from katiesmillie/add border to an image swift 3
Last active
November 27, 2022 18:40
-
-
Save andresr-dev/5fdc3efde8612bb61bb741ddc117f5f4 to your computer and use it in GitHub Desktop.
This is how you can add border to a UIImage
This file contains hidden or 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
import UIKit | |
extension UIImage { | |
func addBorder(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