How to change UITableViewCell Image to Circle in UITableView

前端 未结 11 1509

My code works for the most part.

  • The issue I am having is the images start out as square, and change to circle when I scroll the table or refresh it.

相关标签:
11条回答
  • 2021-02-12 12:42

    Create a custom cell, and add an image view (with an IBOutlet), sized and positioned however you want (the outlet is "iv" in my example below). Put the code to make image view round in the awakeFromNib method (assuming your cell is created in a nib or storyboard).

    -(void)awakeFromNib {
        self.iv.layer.cornerRadius = self.iv.frame.size.width / 2.0;
        self.iv.layer.borderWidth = 3.0;
        self.iv.layer.borderColor = [UIColor colorWithRed:157.0/255.0 green:34.0/255.0 blue:53.0/255.0 alpha:1.0].CGColor;
        self.iv.clipsToBounds = YES;
    }
    
    0 讨论(0)
  • 2021-02-12 12:44

    I had a similar issue, my UIImageView was initially a square and would only show as circular when the UITableViewCell it was within was either highlighted or selected.

    It was a simple fix:

    Swift 2.0

    imgView.layer.cornerRadius = imgView.frame.width / 2
    imgView.clipsToBounds = true
    

    This was placed inside the awakeFromNib() for my custom UITableViewCell.

    This is assuming imgView is the name of the UIImageView hooked up via storyboard inside this custom UITableViewCell.

    0 讨论(0)
  • 2021-02-12 12:48
    @interface MyCell : UITableViewCell
    @end
    
    @implementation MyCell
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
        self.imageView.layer.masksToBounds = YES;
        self.textLabel.font = [UIFont systemFontOfSize:17];
        self.textLabel.textColor = [UIColor darkTextColor];
    }
    
    // --- image view frame is empty rect inside tableView: willDisplayCell:
    // +++ set cornerRadius inside layoutSubviews works. 
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // layout image view
        CGRect vfr = self.frame;
        CGRect imgvr = self.imageView.frame;
        imgvr.origin.x = 16;
        imgvr.size.width = CGRectGetHeight(vfr);
        self.imageView.frame = imgvr;
    
        // update corner radius
        self.imageView.layer.cornerRadius = imgvr.size.width * 0.5f;
    
        // layout label
        CGRect lblr = self.textLabel.frame;
        lblr.origin.x = CGRectGetMaxX(imgvr) + 16;
        lblr.size.width = CGRectGetWidth(vfr) - CGRectGetMaxX(imgvr) - 32;
        self.textLabel.frame = lblr;
    }
    
    @end
    
    0 讨论(0)
  • 2021-02-12 12:49
    cell.imageView?.layer.cornerRadius = 22.0
    cell.imageView?.layer.masksToBounds = true
    
    0 讨论(0)
  • 2021-02-12 12:52

    In order if anyone having this problem, here is the solution in

    Swift

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:ContactCellTableViewCell? = tableView.dequeueReusableCellWithIdentifier("contactCell", forIndexPath: indexPath) as? ContactCellTableViewCell
    
    
    
        var contact : ContactStruct
        image = searchResults[indexPath.row]
        let newImage = resizeImage(image, toTheSize: CGSizeMake(70, 70))
        var cellImageLayer: CALayer?  = cell?.imageView.layer
        cellImageLayer!.cornerRadius = 35
        cellImageLayer!.masksToBounds = true
        cell?.imageView.image = newImage
    
    
        return cell!
    }
    

    As you might noticed the 35 hardcode is basically half of the image size which is 70 in here. And here is the resize function:

    func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{
    
    
        var scale = CGFloat(max(size.width/image.size.width,
            size.height/image.size.height))
        var width:CGFloat  = image.size.width * scale
        var height:CGFloat = image.size.height * scale;
    
        var rr:CGRect = CGRectMake( 0, 0, width, height);
    
        UIGraphicsBeginImageContextWithOptions(size, false, 0);
        image.drawInRect(rr)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();
        return newImage
    }
    

    Note: I am using a custom cell class as below:

    import UIKit
    
    class ContactCellTableViewCell: UITableViewCell {
    
        @IBOutlet weak var nameLabel: UILabel!
        @IBOutlet weak var contactImage: UIImageView!
        @IBOutlet weak var phoneLabel: UILabel!
    
    
    
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
        }
    
        override func setSelected(selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    }
    

    It really took me a while to figure this one out. The images become rounded after scrolling at first, but now using this code you are going to have the rounded images inside the cell from beginning. Hope it helped anyone.

    0 讨论(0)
提交回复
热议问题