简体   繁体   English

如何在UIImageView中获取圆形图像

[英]How to get a circle image in UIImageView

I tried to get a circle image using layer, here is my code 我尝试使用图层获取圆形图像,这是我的代码

_compassArrow.layer.cornerRadius = _compassArrow.frame.size.width / 2;
_compassArrow.layer.masksToBounds = YES;
_compassArrow.layer.borderColor = [UIColor whiteColor].CGColor

the compassArrow is an imageview which display the compass image. 罗盘箭头是一个图像视图,显示罗盘图像。 And when I run my program, it looks terrible: 当我运行程序时,它看起来很糟糕:

my actual picture 我的真实照片

I don't know what happened to it. 我不知道发生了什么事。 I've add some constraints to it, to make it has equal width with the device. 我添加了一些约束,以使其与设备的宽度相等。 Does this influence my image? 这会影响我的形象吗?

I think you set cornerRadius before your constraints are applied. 我认为您在应用约束之前设置了cornerRadius。 Try to put this code in layoutSubviews or viewDidLayoutSubviews for example. 例如,尝试将此代码放入layoutSubviewsviewDidLayoutSubviews中。

This way, the _compassArrow.frame.size.width value will be the one after constraints applied on it, and you'll get the correct cornerRadius. 这样, _compassArrow.frame.size.width值将成为应用约束后的值,您将获得正确的cornerRadius。

Here is a piece of code that should allow you to do this. 这是一段代码,应允许您执行此操作。

        _compassArrow.layer.borderWidth = 1.0
        _compassArrow.layer.masksToBounds = false
        _compassArrow.layer.borderColor = UIColor.whiteColor().CGColor
        _compassArrow.layer.cornerRadius = profilePicture.frame.size.width/2
        _compassArrow.clipsToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM