2013-10-28 35 views
22

我有一个联系人列表的uitableview。我希望将每个联系人的图像放在一个圈框中,就像在特立独行的登录屏幕中一样。任何建议如何在uitableviewCell中做到这一点?圆形框架中的图像iOS

enter image description here

+0

如果我的回答围绕你的问题接受PLZ –

回答

76

我相信你有你的imageView在你的Cell中,所以你只需要做一个效果就是使用一个不可见的边框。使用此代码使这个效果:

cell.yourImageView.layer.cornerRadius = cell.yourImageView.frame.size.height /2; 
cell.yourImageView.layer.masksToBounds = YES; 
cell.yourImageView.layer.borderWidth = 0; 

你的UIImageView必须具有相同的高度和宽度,你也必须导入quartzCore框架项目

+0

是否可以在故​​事板中设置这些更改?或唯一的解决方案是在代码? –

+0

唯一的代码解决方案:/ –

+0

当它处于滚动视图(例如uitableview)时,请考虑性能。 – ZYiOS

1

显然,我可以做一个圆角的矩形这样

float fw, fh; 
if (ovalWidth == 0 || ovalHeight == 0) { 
    CGContextAddRect(context, rect); 
    return; 
} 
CGContextSaveGState(context); 
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); 
CGContextScaleCTM (context, ovalWidth, ovalHeight); 
fw = CGRectGetWidth (rect)/ovalWidth; 
fh = CGRectGetHeight (rect)/ovalHeight; 
CGContextMoveToPoint(context, fw, fh/2); 
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); 
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); 
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); 
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); 
CGContextClosePath(context); 
CGContextRestoreGState(context); 

,然后把图像里面,它会自动裁剪:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(context); 
addRoundedRectToPath(context, self.frame, 10, 10); 
CGContextClip(context); 
[image drawInRect:self.frame]; 
CGContextRestoreGState(context);