【问题标题】:image in circle frame iOS [closed]圆形框架iOS中的图像[关闭]
【发布时间】:2013-10-28 17:01:31
【问题描述】:

我有一个带有联系人列表的 uitableview。我想在小牛的登录屏幕中将每个联系人的图像放在一个圆形框架中。任何建议如何在 uitableviewCell 中执行此操作?

【问题讨论】:

  • 如果你用我的回答来回答你的问题,请接受它

标签: ios objective-c xcode uiimageview uiimage


【解决方案1】:

我假设你的 Cell 中有你的 imageView,所以你要做的就是使用一个不可见的边框来达到同样的效果。使用此代码制作此效果:

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

您的 UIImageView 必须具有相同的高度和宽度,并且您必须在项目中导入quartzCore 框架

【讨论】:

  • 是否可以在情节提要中设置这些更改?还是唯一的解决方案是代码?
  • 只有代码解决方案:/
  • 在滚动视图(如uitableview)时请考虑性能。
  • 工作就像一个魅力!太好了 - 谢谢!
  • 如果您使用情节提要原型作为单元格,您可以在“用户定义的运行时属性”中设置这些属性
【解决方案2】:

显然我可以像这样制作一个圆角矩形

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2019-11-12
    • 2012-12-12
    • 2018-08-05
    • 2016-05-07
    相关资源
    最近更新 更多