gpt4 book ai didi

swift - 从 imagePickerController 选择的图像占据整个屏幕,而不是适合 UI ImageView [Swift]

转载 作者:行者123 更新时间:2023-11-30 12:25:12 26 4
gpt4 key购买 nike

我有一个在容器 View 中以编程方式创建的 ImageView ,如下所示:

Screen shot with circular profile image view inside container view

我在个人资料 ImageView 顶部有一个按钮覆盖层,用于调用图像选择器 Controller 。当从照片库中选择图像时,该图像会占据整个屏幕,如下所示:

Screenshot after image is selected

以下是该 View Controller 的 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)
}

最佳答案

你可以试试这个

self.profileImage.contentMode = .scaleToFill

关于swift - 从 imagePickerController 选择的图像占据整个屏幕,而不是适合 UI ImageView [Swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com