How to change UITableViewCell Image to Circle in UITableView

前端 未结 11 1510

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: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];
            });
        }
    });
    

提交回复
热议问题