I'm trying to put a Circular UIImageview into a UITableViewCell and I'm not having much success :(.
In my TableViewCell class
class ComentariosTVCell: UITableViewCell {
@IBOutlet weak var photoUser: UIImageView!
//weak var photoUser: UIImageView!
override func layoutSubviews() {
photoUser.round()
/*
photoUser.layer.borderWidth = 1
photoUser.layer.masksToBounds = true
photoUser.layer.borderColor = UIColor.blackColor().CGColor
photoUser.layer.cornerRadius = 40.0 //photoUser.bounds.height/2
print("frame: ", photoUser.frame)
photoUser.clipsToBounds = true*/
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}}
public extension UIView {
public func round() {
let width = bounds.width < bounds.height ? bounds.width : bounds.height
let mask = CAShapeLayer()
mask.path = UIBezierPath(ovalInRect: CGRectMake(bounds.midX - width / 2, bounds.midY - width / 2, width, width)).CGPath
self.layer.mask = mask
}}
and when loading the table the view does not load correctly if not until one of the cells is selected or scrool is done.
where am I failing?
Perhaps you are complicating yourself. The simplest and fastest thing you can do is the following:
Basically what you're doing here is rounding the ,
UIImageView
and making suremaskToBounds
the image doesn't go outside theUIImageView
. This is probably the best way and the one that will give you the best performance.UPDATE 1
You can also do the implementation in the subclass directly