Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set imageView in circle like imageContacts in Swift correctly?

I want to show a picture into imageView like the image contact (in a circle) But when I try to show this, the imageView rescale his size and this doesn't show correctly in a circle.

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

I want to show like this: enter image description here

But I get this: enter image description here

How can do the image resize to UIImageView size to show as a circle?

Thanks!

like image 613
user3745888 Avatar asked Aug 30 '14 23:08

user3745888


2 Answers

This is solution which I have used in my app:

var image: UIImage = UIImage(named: "imageName") imageView.layer.borderWidth = 1.0 imageView.layer.masksToBounds = false imageView.layer.borderColor = UIColor.whiteColor().CGColor imageView.layer.cornerRadius = image.frame.size.width/2 imageView.clipsToBounds = true 

Swift 4.0

let image = UIImage(named: "imageName") imageView.layer.borderWidth = 1.0 imageView.layer.masksToBounds = false imageView.layer.borderColor = UIColor.white.cgColor imageView.layer.cornerRadius = image.frame.size.width / 2 imageView.clipsToBounds = true 
like image 187
Manu Gupta Avatar answered Oct 21 '22 19:10

Manu Gupta


What frame size are you using for image? I can get a perfect circle if I set the frame to be a square.

let image = UIImageView(frame: CGRectMake(0, 0, 100, 100)) 
like image 36
chazkii Avatar answered Oct 21 '22 19:10

chazkii