【问题标题】:Image from UIImagePicker Orientation Incorrect来自 UIImagePicker 的图像方向不正确
【发布时间】:2017-06-22 21:41:09
【问题描述】:

我正在为我的 swift 应用程序创建一个注册页面,该页面包含一个允许用户选择个人资料图片的按钮,一旦用户选择了该图片,该图片应替换占位符“添加图片”按钮,通过将 button.setImage() 与新选择的图片一起使用。我的问题是当用户选择图片时,它在屏幕上完全水平拉伸,而不是限制在按钮的 150x150 宽度和高度。

这是我的按钮:

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
}()

我像这样将它添加到视图中:

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)
}

使用这个 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
    }
}

}

我这样展示我的 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)
}

感谢您的任何帮助,谢谢!

【问题讨论】:

    标签: ios swift xcode uiimage uiimagepickercontroller


    【解决方案1】:

    没关系 - 刚刚检查了我的扩展并且没有将我的宽度约束的 isActive 属性设置为 true。呵呵

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2020-09-21
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多