如何用Swift正确设置圆形图像?

时间:2015-08-05 04:11:48

标签: ios swift uiimageview

我试图制作一个圆形图像配置文件,就像instagram / whatsapp的个人资料图片一样。现在我的代码似乎工作,但我用两种不同的方式做到了,两者都有效,所以我想知道哪一个是最好的

第一种方式:

profileImageView.layer.cornerRadius = profileImageView.frame.width / 2
profileImageView.clipsToBounds = true

第二种方式

profileImageView.layer.cornerRadius = profileImageView.frame.width / 2
profileImageView.layer.masksToBounds = true

另外我想如果有人可以向我解释" clipToBounds"和" maskToBounds",他们做了什么。谢谢!

2 个答案:

答案 0 :(得分:0)

我一直这样做的方式,特别是在我想在我的应用程序中显示个人资料图片的情况下,我使用此代码:

profileImage.layer.cornerRadius = self.profileImage.frame.size.width / 2;
profileImage.clipsToBounds = true;

我建议这样做!

答案 1 :(得分:0)

clipsToBounds是一个布尔值,用于确定子视图是否仅限于视图的边界。 将此值设置为YES会导致子视图被剪切到接收器的边界。如果设置为NO,则不会剪切其帧超出接收器可见边界的子视图。默认值为NO。 基本上,这个东西与视图的属性一起使用。

而maskToBounds是一个布尔值,表示子图层是否被剪裁到图层的边界。这个东西与视图层一起使用。