I have been trying to mask a UIImage into a circle. I\'m using now the code that has been popular on other answers here, but although I do get a circle its edges are very ja
Felt like I should throw my hat into the ring. An effective, compact Swift 5 solution:
extension UIImage {
var rounded: UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
defer { UIGraphicsEndImageContext() }
let bounds = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: bounds, cornerRadius: size.height/2.0).addClip()
draw(in: bounds)
return UIGraphicsGetImageFromCurrentImageContext()
}
}