How to set image in circle in swift

前端 未结 14 1696
星月不相逢
星月不相逢 2020-12-04 09:55

How can i make i circle picture with swift ?

My ViewController :

import UIKit
import Foundation

class FriendsViewController : UIViewController{

            


        
相关标签:
14条回答
  • 2020-12-04 09:58

    First you need to set equal width and height for getting Circular ImageView.Below I set width and height as 100,100.If you want to set equal width and height according to your required size,set here.

     var imageCircle = UIImageView(frame: CGRectMake(0, 0, 100, 100))
    

    Then you need to set height/2 for corner radius

     imageCircle.layer.cornerRadius = imageCircle.frame.size.height/2
     imageCircle.layer.borderWidth = 1
     imageCircle.layer.borderColor = UIColor.blueColor().CGColor
     imageCircle.clipsToBounds = true
     self.view.addSubview(imageCircle)
    
    0 讨论(0)
  • 2020-12-04 09:58
    struct CircleImage: View {
        var image: Image
    
        var body: some View {
            image
               .clipShape(Circle())
        }
    }
    

    This is correct for SwiftUI

    0 讨论(0)
  • 2020-12-04 09:59

    Don't know if this helps anyone but I was struggling with this problem for awhile, none of the answers online helped me. For me the problem was I had different heights and widths set on the image in storyboard. I tried every solution on stack and it turns out it was something as simple as that. Once I set them both to 200 my circle profile image was perfect. This was code then in my VC.

    profileImage2.layer.cornerRadius = profileImage2.frame.size.width/2
        profileImage2.clipsToBounds = true
    
    0 讨论(0)
  • 2020-12-04 10:01

    You can simple create extension:

    import UIKit
    
    extension UIImageView {
    
       func setRounded() {
          let radius = CGRectGetWidth(self.frame) / 2
          self.layer.cornerRadius = radius
          self.layer.masksToBounds = true
       }
    }
    

    and use it as below:

    imageView.setRounded()
    
    0 讨论(0)
  • 2020-12-04 10:02

    All the answers above couldn't solve the problem in my case. My ImageView was placed in a customized UITableViewCell. Therefore I had also call the layoutIfNeeded() method from here. Example:

     class NameTableViewCell:UITableViewCell,UITextFieldDelegate { ...
    
     override func awakeFromNib() {
    
        self.layoutIfNeeded()
        profileImageView.layoutIfNeeded()
    
        profileImageView.isUserInteractionEnabled = true
        let square = profileImageView.frame.size.width < profileImageView.frame.height ? CGSize(width: profileImageView.frame.size.width, height: profileImageView.frame.size.width) : CGSize(width: profileImageView.frame.size.height, height:  profileImageView.frame.size.height)
        profileImageView.addGestureRecognizer(tapGesture)
        profileImageView.layer.cornerRadius = square.width/2
        profileImageView.clipsToBounds = true;
    }
    
    0 讨论(0)
  • 2020-12-04 10:05

    This way is the least expensive way and always keeps your image view rounded:

    class RoundedImageView: UIImageView {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            clipsToBounds = true
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            clipsToBounds = true
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            assert(bounds.height == bounds.width, "The aspect ratio isn't 1/1. You can never round this image view!")
    
            layer.cornerRadius = bounds.height / 2
        }
    }
    

    The other answers are telling you to make views rounded based on frame calculations set in a UIViewControllers viewDidLoad() method. This isn't correct, since it isn't sure what the final frame will be.

    0 讨论(0)
提交回复
热议问题