Rounded Corners on UIImage

前端 未结 12 1063
终归单人心
终归单人心 2020-12-02 04:08

I\'m trying to draw images on the iPhone using with rounded corners, a la the contact images in the Contacts app. I\'ve got code that generally work, but it occasionally cra

12条回答
  •  无人及你
    2020-12-02 04:47

    In Swift 4.2 and Xcode 10.1

    let imgView = UIImageView()
    imgView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
    imgView.image = UIImage(named: "yourimagename")
    imgView.imgViewCorners()
    //If you want complete round shape
    //imgView.imgViewCorners(width: imgView.frame.width)//Pass ImageView width
    view.addSubview(imgView)
    
    extension UIImageView {
    //If you want only round corners
    func imgViewCorners() {
        layer.cornerRadius = 10
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
    //If you want complete round shape
    func imgViewCorners(width:CGFloat) {
        layer.cornerRadius = width/2
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.red.cgColor
        layer.masksToBounds = true
    }
    

提交回复
热议问题