ImagePicker确认视图对齐

时间:2019-04-14 23:00:14

标签: swift uiimagepickercontroller

我正在使用UIImagePickerController拍照。拍摄完照片后,我将带您确认拍摄或重新拍摄的景色。该视图的对齐方式不同,似乎与屏幕底部对齐。有没有办法将两个视图都对齐到屏幕顶部?我可以跳过此步骤吗?

Screenshot of both views

imagePickerController = UIImagePickerController()
imagePickerController.delegate = self

//check if camera is available
if UIImagePickerController.isSourceTypeAvailable(.camera) {
    imagePickerController.sourceType = .camera
} else {
    imagePickerController.sourceType = .photoLibrary
}

self.present(imagePickerController, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:0)

那么,您无法使用自定义控件显示该确认屏幕。这是一个非常简单的示例:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  @IBOutlet weak var textField: UITextField!
  var imagePickerController: UIImagePickerController!

  override func viewDidLoad() {
    super.viewDidLoad()

    guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return }
    imagePickerController = UIImagePickerController()
    imagePickerController.sourceType = .camera
    imagePickerController.showsCameraControls = false
    imagePickerController.delegate = self

    // Button to take picture
    let takePictureButton = UIButton()
    takePictureButton.setTitle("Take Picture", for: .normal)
    imagePickerController.view.addSubview(takePictureButton)

    // Positioning button
    takePictureButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      takePictureButton.centerXAnchor.constraint(equalTo: imagePickerController.view.centerXAnchor),
      takePictureButton.bottomAnchor.constraint(equalTo: imagePickerController.view.bottomAnchor, constant: -50)
      ])

    // Set button target
    takePictureButton.addTarget(self, action: #selector(didTouchTakePictureButton(_:)), for: .touchUpInside)

    self.present(imagePickerController, animated: true, completion: nil)
  }

  @objc func didTouchTakePictureButton(_ sender: UIButton) {
    imagePickerController.takePicture()
  }

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    guard let image = info[.originalImage] as? UIImage else { return }
  }
}

但是请注意以下几点:  1.您只能使用.camera作为来源类型;  2.您没有任何控件,因此您必须自己添加所有自定义;

希望这会有所帮助!