Search code examples
iosswiftuitableviewuiimagecell

UITableView - UITableViewCell.Style .subtitle - How to make UIImageView circular? - Swift 5


I got a UITableView with default cell styles. (set to .subtitle style) I want to make its UIImageView circular.

cell.imageView.layer.borderWidth = 1
cell.imageView.layer.masksToBounds = false
cell.imageView.layer.borderColor = UIColor.white.cgColor
cell.imageView.layer.cornerRadius = profileImageView.frame.size.width/2
cell.imageView.clipsToBounds = true

this would not work at this situation!


Solution

  • I found the right way thanks to https://stackoverflow.com/a/50462058/10489699 .

            let itemSize = CGSize.init(width: 50, height: 50)
            UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale)
            let imageRect = CGRect.init(origin: CGPoint.zero, size: itemSize)
            cell.imageView?.image!.draw(in: imageRect)
            cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            cell.imageView?.layer.cornerRadius = (itemSize.width) / 2
            cell.imageView?.clipsToBounds = true
    

    Don't try if let image = cell.imageView?.image! {...} , I don't know why but it hits the UIImageView !