UIImage in a circle

2020-02-20 08:25发布

Can somebody help me here?. New as iPhone Developer. I am trying to display a .png picture in a circle instead of a rectangle which is the standard for iPhone

8条回答
Melony?
2楼-- · 2020-02-20 08:49

try this code

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

this show image like ios 7 circle image thanks

查看更多
Melony?
3楼-- · 2020-02-20 08:49

Changing the cornerRadius of the image view works well if you have only a couple of images in your view. However if the image view is in a tableview performance will be affected.

Some of the other options:

  1. Make the image assets circle on the server or manually if they are bundled into the app, with transparent parts for the outer region of the circle.
  2. If the background of the image view doesn't change, create an overlay image that has the inner circle part transparent, and the rest to be the same as the background. Also set the backgroundColor of the image view to clearColor.
  3. When receiving the images, edit them in code to be a circle, in a background thread.
查看更多
男人必须洒脱
4楼-- · 2020-02-20 08:53

I'll add a slightly more universal extension to UIImageView that will work with non-square images. To be noted that it will work slower than the cornerRadius method.

extension UIImageView {
    @IBInspectable public var asEllipse:Bool {
        get {
            if let mask = self.layer.mask {
                return mask.name == kMaskLayerName
            }
            return false;
        }

        set {
            if (newValue) {
                let ellipseMask = CAShapeLayer()
                ellipseMask.name = kMaskLayerName
                ellipseMask.path = CGPathCreateWithEllipseInRect(self.bounds, nil)
                ellipseMask.strokeColor = UIColor.clearColor().CGColor
                ellipseMask.fillColor = UIColor.whiteColor().CGColor
                self.layer.mask = ellipseMask
            } else if self.asEllipse {
                self.layer.mask = nil
            }
        }
    }

}

private let kMaskLayerName="EllipseMaskLayer"
查看更多
Viruses.
5楼-- · 2020-02-20 08:55

Use a UIImageView and set the cornerRadius to be half height and width. view.layer.cornerRadius = cornerRadius;

UIImage rounded corners

查看更多
Evening l夕情丶
6楼-- · 2020-02-20 08:56

Swift 4: This should display your .png in a circle.

  1. Drag (ctrl + click) an IBOutlet of an image toward your code.

enter image description here

cornerRadius The radius to use when drawing rounded corners for the layer’s background. Animatable. https://developer.apple.com/documentation/quartzcore/calayer/1410818-cornerradius

clipsToBounds property A Boolean value that determines whether subviews are confined to the bounds of the view. https://developer.apple.com/documentation/uikit/uiview/1622415-clipstobounds

2.Inside viewDidLoad(), Use the instance property layer.cornerRadius and clipsToBounds.

profileImage.layer.cornerRadius = 50
profileImage.clipsToBounds = true
查看更多
来,给爷笑一个
7楼-- · 2020-02-20 08:59

My contribution with a swift extension used to set an UIImageView as circle

extension UIImageView{

    func asCircle(){
        self.layer.cornerRadius = self.frame.width / 2;
        self.layer.masksToBounds = true
    }

}

Just call MyImageView.asCircle()

查看更多
登录 后发表回答