Make images round in swift?

You might be missing something, below code works for me:

            profilePicture.layer.borderWidth=1.0
            profilePicture.layer.masksToBounds = false
            profilePicture.layer.borderColor = UIColor.whiteColor().CGColor
            profilePicture.layer.cornerRadius = profilePicture.frame.size.height/2
            profilePicture.clipsToBounds = true

Note: To get a perfect circle, the image view frame should be square.


It's displaying a diamond shape because you're setting the cornerRadius before the the size of the view changes.

This would result in a diamond shape:

var myView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
myView.backgroundColor = UIColor.redColor()
view.addSubview(myView)
myView.layer.cornerRadius = myView.frame.size.width / 2
// setting frame doesn't change corner radius from the former large value
myView.frame = CGRect(x: 50, y: 50, width: 50, height: 50)

You can set this immediately before the view is displayed by doing so in viewWillAppear:

Tags:

Ios

Swift

Xcode6