UITableView - UITableViewCell.Style .subtitle - 如何使 UIImageView 循环? - Swift 5

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

我得到了一个带有默认单元格样式的 UITableView。 (设置为 .subtitle 样式) 我想让它 UIImageView 循环。

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

这在这种情况下不起作用!

您可以使用此代码

public extension UIView {
    func round() {
        let width = bounds.width < bounds.height ? bounds.width : bounds.height
        let mask = CAShapeLayer()
        mask.path = UIBezierPath(ovalIn: CGRect(x: bounds.midX - width / 2, y: bounds.midY - width / 2, width: width, height: width)).cgPath
        self.layer.mask = mask
    }
}

你可以这样使用:

profileImageView.round()

感谢,我找到了正确的方法。

        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

不要尝试 if let image = cell.imageView?.image! {...} ,我不知道为什么但它会影响 UIImageView !