Make images round in swift?
I am trying to make images round and for some reason the below code creates a diamond shaped photo:
profilePicture.layer.cornerRadius = profilePicture.frame.size.width / 2
profilePicture.clipsToBounds = true
How should I do it? Thank!
It displays the shape of the diamond because you set the cornerRadius before resizing the view.
This will 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 just before the view is rendered by doing this in viewWillAppear:
Maybe you are 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 frame of the image must be square.
Just write the following code in viewdidLoad
after setting the image limit
self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width/2 ;
self.profileImageView.clipsToBounds = YES;
To get any UIImageview
in a round shape, you need to first set its Frame to a square shape. For example, if you UIImageView
have a Frame, (20,20,100,100)
you will see that the height and width are the same. Then you need to set the cornerradius
angle of this to UIImageview
half the size of its height or width .
I did something like this in Xamarin.iOS: -
imageView.Layer.CornerRadius = imageView.Frame.Size.Width / 2;
imageView.ClipsToBounds = true;
try it worked for me, i just added myself. in front of the teams
self.profilePicture.frame.size.width / 2;
self.profilePicture.clipsToBounds = true;
hope it works :)