Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Forked from katiesmillie/add border to an image swift 3
Last active November 27, 2022 18:40
Show Gist options
  • Save andresr-dev/5fdc3efde8612bb61bb741ddc117f5f4 to your computer and use it in GitHub Desktop.
Save andresr-dev/5fdc3efde8612bb61bb741ddc117f5f4 to your computer and use it in GitHub Desktop.
This is how you can add border to a UIImage
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