Ios 如何在UITableView中将UITableViewCell图像更改为圆形

Ios 如何在UITableView中将UITableViewCell图像更改为圆形,ios,objective-c,uitableview,uiimageview,Ios,Objective C,Uitableview,Uiimageview,我的代码大部分是有效的 我遇到的问题是,图像以正方形开始,在滚动表格或刷新表格时变为圆形 我错过了什么 这是我的代码: cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0; cell.imageView.layer.borderWidth = 3.0; cell.imageView.layer.borderColor = [UIColor colorWithRed:157.

我的代码大部分是有效的

  • 我遇到的问题是,图像以正方形开始,在滚动表格或刷新表格时变为圆形
我错过了什么

这是我的代码:

    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;

    NSDictionary *tweet = (self.results)[indexPath.row];
    
    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 setNeedsLayout];
            });
        }
    });
EDIT——代码位于
cellForRowAtIndexPath

  • 就在我启动应用程序时,手机图像是方形的。如果我
    pullToRefresh
    将更改为圆形,或者如果我开始滚动表格,它们将更改为圆形
编辑两个

我看到的另一个问题是图像在滚动时会发生变化


如何防止这种情况发生?

最有可能的情况是,单元格的帧(以及imageView的帧)在第一次创建单元格时没有设置。因此,基于初始帧设置imageView层的
拐角半径
不起作用。最好的解决方案是实现
tableView:willDisplayCell:forrowatinexpath:
delegate方法,并在其中设置层:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2.0;
}

创建一个自定义单元格,并添加一个图像视图(带有
IBOutlet
),其大小和位置随您的需要而定(在下面的示例中,插座为“iv”)。将代码放入
awakeFromNib
方法中,使图像视图为圆形(假设您的单元格是在nib或故事板中创建的)

这很简单

- (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;
}

有关更多信息,请参阅此链接:

如果有人遇到此问题,请参阅中的解决方案

迅捷的

正如您可能注意到的,35硬代码基本上是图像大小的一半,在这里是70。 以下是调整大小功能:

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
}
注意:我使用的自定义单元格类如下所示:

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
    }
}
我花了很长时间才弄明白这一点。开始滚动后,图像会变圆,但现在使用此代码,您将从一开始在单元格中获得圆角图像。
希望它对任何人都有帮助。

在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
}

我有一个类似的问题,我的
UIImageView
最初是一个正方形,只有当它所在的
UITableViewCell
突出显示或选中时才会显示为圆形

这是一个简单的修复:

Swift 2.0

imgView.layer.cornerRadius = imgView.frame.width / 2
imgView.clipsToBounds = true
对于我的自定义
UITableViewCell
,它被放置在
awakeFromNib()中


这是假设
imgView
是通过自定义
UITableViewCell
内的故事板连接的
UIImageView
的名称,将图像视图设置为
async
调用内的一个圆,在该调用中设置图像

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

对于
UITableview
单元格中的圆形图像,这里有一个完美的状态转移解决方案。 只需使用以下代码修改
UITableviewCell
(自定义单元格)类

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
  }

如果您的单元格是按代码显示的,则只有在滚动表格(如果有)后,才有助于解决图像循环问题。

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
 }

您在哪里有此代码?您发布的代码表明您希望图像视图为圆形,对吗?所以问题是它们一开始不是圆形的?它在cellForRowAtIndexPath中。是的,我希望它们始终是圆形的,但它们开始时是方形的,如果我刷新或滚动,它们会不断更新。这里的每一项都取决于行高。因此,如果行高为100,只需将cell.imageView.layer.cornerRadius设置为50;这意味着总是行高/2。我也遇到同样的问题。有时
UIImageView
是圆的,有时不是。但我发现如果图像的高度和宽度等于单元格的高度,它可以工作。当我实现此功能时,仍然以正方形开始。你从
cell.imageView
cell.imageView.frame中得到了什么?cell.imageView:和cell.imageView.frame:0.000000当我刷新时:cell.imageView:和cell.imageView.frame:4294968343.000000你可以将虚拟图像设置为没有图像的新单元格。这应该初始化
imageView
帧,以便角落半径计算按预期工作。单元格已经带有
imageView
属性。到目前为止的问题是,默认imageView的帧在获得图像之前是不会设置的。@rmaddy,是的,我知道单元格带有图像视图属性。它就像我在这里做的那样,没有图像显示。另外,我通常不喜欢在表视图的委托/数据源中设置不依赖于XPath的内容。我认为这些东西应该属于单元,所以这就是我提出这种方法的原因。这很有效,但前提是您在故事板中明确指定
imageView
的高度。请在您的回答中描述问题所在,以及此片段将如何解决问题,以帮助其他人理解此答案
@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
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
  }
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
 }
cell.imageView?.layer.cornerRadius = 22.0
cell.imageView?.layer.masksToBounds = true