圆形框架中的图像 iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19640766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:39:07  来源:igfitidea点击:

image in circle frame iOS

iosobjective-cxcodeuiimageviewuiimage

提问by jonas vermeulen

i have uitableview with a list of contact. I would like to have the image of each contact in an circle frame like in the login screen of mavericks. Any suggestions how to do this in an uitableviewCell ?

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

enter image description here

在此处输入图片说明

回答by Mirko Catalano

I presume that you have your imageView inside your Cell, so what your have just to do for have the same effect is use a invisible border. Use this code for make this effect:

我认为您的单元格内有 imageView,所以您只需要做的就是使用不可见的边框来实现相同的效果。使用此代码实现此效果:

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

Your UIImageView must have the same height and width also you have to import the quartzCore framework in your project

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

回答by jonas vermeulen

apparently i can make a rounded rect like this

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

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

and then put the image inside, it will be automatically clipped:

然后把图片放进去,它会被自动剪裁:

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