【问题标题】:Image selected from imagePickerController takes up whole screen instead of fitting inside of UI Image View [Swift]从 imagePickerController 中选择的图像占据整个屏幕,而不是适合 UI Image View [Swift]
【发布时间】:2017-06-01 05:03:38
【问题描述】:

我有一个在容器视图中以编程方式创建的图像视图:

Screen shot with circular profile image view inside container view

我在配置文件图像视图顶部有一个按钮覆盖来调用图像选择器控制器。当从照片库中选择图片时,图片会占据整个屏幕,如下所示:

Screenshot after image is selected

这里是这个视图控制器的 viewDidLoad() 和 imagePickerController() 的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    self.profileBackgroundView.backgroundColor = UIColor.init(red: CGFloat(69.0/255.0), green: CGFloat(171.0/255.0), blue: CGFloat(222.0/255.0), alpha: CGFloat(1))

    //initializePrimaryUserData()

    fullNameTextField.text = self.primaryUser.firstName + self.primaryUser.lastName
    nickNameTextField.text = self.primaryUser.userName

    //create profile image view

    if hasProfilePic {
        profileImage.image = self.primaryUser.image
    } else {
        image = UIImage(named: imageName)!
        profileImage.image = image
    }

    profileImage.frame = CGRect(x: profileBackgroundView.bounds.midX, y: profileBackgroundView.bounds.midY, width: 100, height: 100)

    profileImage.translatesAutoresizingMaskIntoConstraints = false
    profileBackgroundView.addSubview(profileImage)

    profileImage.centerXAnchor.constraint(equalTo: profileBackgroundView.centerXAnchor).isActive = true
    profileImage.centerYAnchor.constraint(equalTo: profileBackgroundView.centerYAnchor).isActive = true

    profileImage.layer.cornerRadius = profileImage.frame.size.width / 2
    profileImage.layer.masksToBounds = false
    profileImage.clipsToBounds = true
    profileImage.contentMode = .scaleAspectFit

    //create profileImageButton
    let profileImageButton = UIButton(frame: CGRect(x: profileBackgroundView.bounds.midX, y: profileBackgroundView.bounds.midY, width: 100, height: 100))
    profileImageButton.setTitle("Photo", for: .normal)
    profileImageButton.addTarget(self, action: #selector(profileImageButtonClicked), for: .touchUpInside)

    profileImageButton.translatesAutoresizingMaskIntoConstraints = false

    profileBackgroundView.addSubview(profileImageButton)

    profileImageButton.centerXAnchor.constraint(equalTo: profileBackgroundView.centerXAnchor).isActive = true
    profileImageButton.centerYAnchor.constraint(equalTo: profileBackgroundView.centerYAnchor).isActive = true

    // Do any additional setup after loading the view.

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

        self.primaryUser.image = image
        self.hasProfilePic = true
        self.profileImage.image = image
        self.profileImage.contentMode = .scaleAspectFit
        self.profileImage.clipsToBounds = true
    }

    self.dismiss(animated: true, completion: nil)
}

【问题讨论】:

    标签: swift uiimageview uiimagepickercontroller


    【解决方案1】:

    你可以试试这个

    self.profileImage.contentMode = .scaleToFill
    

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      相关资源
      最近更新 更多