How to change UITableViewCell Image to Circle in UITableView

前端 未结 11 1473

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:32

    Its simple..

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        cell.ImageView.layer.cornerRadius = 150.0f;
        cell.ImageView.layer.borderWidth = 2.0f;
        cell.ImageView.layer.borderColor = [UIColor blackColor].CGColor;
        cell.ImageView.clipsToBounds = YES;
    }
    

    Refer this link for more information: http://ios-blog.co.uk/tutorials/quick-tips/how-to-create-rounded-avatars-in-your-ios-application/

    0 讨论(0)
  • 2021-02-12 12:36

    Most likely the cell's frame (and hence the imageView's frame) isn't set the very first time the cell is created. So setting the imageView's layer's cornerRadius based on that initial frame doesn't work. The best solution is to implement the tableView:willDisplayCell:forRowAtIndexPath: delegate method and set the layer there:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
    }
    
    0 讨论(0)
  • 2021-02-12 12:36

    Set the image view to a circle inside the async call where you set the image.

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
    
        NSString * imageString = [tweet valueForKeyPath:@"user.profile_image_url"];
        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:imageString]];
    
        if (imageData != nil)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
    
                cell.imageView.image = [UIImage imageWithData: imageData];
    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
    cell.imageView.layer.borderWidth = 3.0;
    cell.imageView.layer.borderColor = [UIColor colorWithRed:157.0/255.0 green:34.0/255.0 blue:53.0/255.0 alpha:1.0]
    .CGColor;
    cell.imageView.clipsToBounds = YES;
                [cell setNeedsLayout];
            });
        }
    });
    
    0 讨论(0)
  • 2021-02-12 12:36

    Here is a perfect and state away solution for circular image in UITableview Cell. Simply modify your UITableviewCell (custom cell) class with below code.

    override func awakeFromNib() {
        super.awakeFromNib()
        imgEvent.layer.frame = (imgEvent.layer.frame).insetBy(dx: 0, dy: 0)
        imgEvent.layer.borderColor = UIColor.gray.cgColor
        imgEvent.layer.cornerRadius = (imgEvent.frame.height)/2
        imgEvent.layer.masksToBounds = false
        imgEvent.clipsToBounds = true
        imgEvent.layer.borderWidth = 0.5
        imgEvent.contentMode = UIViewContentMode.scaleAspectFill
      }
    

    It will also helps to solve the problem of image circular only after scrolling table..(if anyenter image description here)

    0 讨论(0)
  • 2021-02-12 12:37

    Try this in override-

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        // image Width(84) / 2 = 42
                cell.CellImage.layer.cornerRadius = 42.0
                cell.CellImage.layer.masksToBounds = true
             //or   
    
            cell.CellImage.layer.cornerRadius = cell.CellImage.frame.width / 2
            cell.CellImage.clipsToBounds = true
    }
    
    0 讨论(0)
  • 2021-02-12 12:40

    In case your cell is by code:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            let myImageView = (cell as! MyCustomCell).myImageView
            myImageView.layoutIfNeeded()
            myImageView.layer.cornerRadius = myImageView.frame.width / 2.0
     }
    
    0 讨论(0)
提交回复
热议问题