Making a UIImage to a circle form

后端 未结 10 2084
南旧
南旧 2020-12-02 17:16

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

10条回答
  •  执念已碎
    2020-12-02 17:33

    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()
        }
    
    }
    
    

提交回复
热议问题