[英]Image from UIImagePicker Orientation Incorrect
I'm creating a sign up page for my swift app which consists of a button that allows the user to pick a profile picture, and that picture should replace the placeholder "add picture" button once the user has selected it, by using button.setImage() with the newly selected picture. 我正在为我的swift应用程序创建一个注册页面,其中包含一个按钮,该按钮允许用户选择个人资料图片,并且一旦用户选择了该图片,就应使用按钮替换该占位符“添加图片”按钮。 setImage()和新选择的图片。 My problem is that when the user selects the picture, it's completely stretched horizontally across the screen instead of constraining to the 150x150 width and height of the button.
我的问题是,当用户选择图片时,图片在屏幕上完全水平拉伸,而不是限制在按钮的宽度和高度为150x150。
This is my button: 这是我的按钮:
let plusPhotoButton : UIButton = {
let b = UIButton(type: .system)
b.imageView?.clipsToBounds = true
b.setImage(#imageLiteral(resourceName: "plus_photo").withRenderingMode(.alwaysOriginal), for: .normal)
b.addTarget(self, action: #selector(setProfilePic), for: .touchUpInside)
return b
}()
I add it to the view like this: 我将其添加到这样的视图中:
fileprivate func setupPlusPhotoButton() {
view.addSubview(plusPhotoButton)
plusPhotoButton.anchor(top: view.topAnchor, paddingTop: 50, left: nil, paddingLeft: 0, right: nil, paddingRight: 0, bottom: nil, paddingBottom: 0, width: 150, height: 150)
plusPhotoButton.anchorCenter(x: view.centerXAnchor, y: nil)
}
Using this UIView extension to make my anchoring easier: 使用此UIView扩展使锚定更容易:
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, paddingTop: CGFloat, left: NSLayoutXAxisAnchor?, paddingLeft: CGFloat, right: NSLayoutXAxisAnchor?, paddingRight: CGFloat, bottom: NSLayoutYAxisAnchor?, paddingBottom: CGFloat, width: CGFloat, height: CGFloat) {
self.translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
}
if let right = right {
self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if let bottom = bottom {
self.bottomAnchor.constraint(equalTo: bottom, constant: paddingBottom).isActive = true
}
if width != 0 {
self.widthAnchor.constraint(equalToConstant: width)
}
if height != 0 {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
func anchorCenter(x: NSLayoutXAxisAnchor?, y: NSLayoutYAxisAnchor?) {
self.translatesAutoresizingMaskIntoConstraints = false
if let x = x {
self.centerXAnchor.constraint(equalTo: x).isActive = true
}
if let y = y {
self.centerYAnchor.constraint(equalTo: y).isActive = true
}
}
} }
And I present my UIImagePickerController like this: 我呈现这样的UIImagePickerController:
func setProfilePic() {
print("setting profile picture")
let imagePickerVC = UIImagePickerController()
imagePickerVC.delegate = self
imagePickerVC.allowsEditing = true
present(imagePickerVC, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
plusPhotoButton.setImage(editedImage.withRenderingMode(.alwaysOriginal), for: .normal)
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
plusPhotoButton.setImage(originalImage.withRenderingMode(.alwaysOriginal), for: .normal)
}
plusPhotoButton.layer.cornerRadius = plusPhotoButton.frame.width/2
plusPhotoButton.layer.masksToBounds = true
plusPhotoButton.layer.borderColor = UIColor.black.cgColor
plusPhotoButton.layer.borderWidth = 3
dismiss(animated: true, completion: nil)
}
Any and all help is appreciated, thank you! 任何和所有帮助表示感谢,谢谢!
Nevermind - just checked my extension and didn't set my width constraint's isActive property to true. 没关系-只是检查了我的扩展名,并且没有将我的宽度约束的isActive属性设置为true。 Duh
咄
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.