Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issues with UIImageView.layer.cornerRadius to create rounded images on different pixel densities ios

I'm simply trying to create a perfectly round image. Here's my swift code:

myImage.layer.cornerRadius = myImage.frame.size.width/2
myImage.layer.masksToBounds = true

This works on a 4s, but is not quite round on a 5s, and appears as a rounded rectangle on a iphone 6.

I'm assuming this has to do with frame.size.width returning values in pixels not points or something like that, but I've been unable to solve this problem.

like image 836
nwales Avatar asked Dec 16 '14 16:12

nwales


1 Answers

If your code is in viewDidLoad in ViewController, try moving it to viewDidLayoutSubviews.

If your rounded imageView is in tableViewCell, try moving it to draw.

override func draw(_ rect: CGRect) {
    avatarView.layer.cornerRadius = avatarView.frame.size.width / 2
    avatarView.layer.masksToBounds = true
    avatarView.clipsToBounds = true
    avatarView.contentMode = .scaleAspectFill
}
like image 148
Phillip Avatar answered Oct 02 '22 04:10

Phillip