简体   繁体   English

相机拍摄的图像未显示在图像视图中

[英]Camera taken image is not showing in image view

I am having an issue while displaying camera captured image in an image view where delegates are set and being called "didFinishPickingMediaWithInfo".我在设置代表并被称为“didFinishPickingMediaWithInfo”的图像视图中显示相机捕获的图像时遇到问题。

I have two buttons for the user interaction (camera picture & from gallery), once I take picture from camera and then select any other image from gallery;我有两个用于用户交互的按钮(相机图片和来自画廊),一旦我从相机拍照,然后 select 来自画廊的任何其他图像; the camera picture taken earlier is shown for a second.之前拍摄的相机照片会显示一秒钟。 I tried searching a lot but no luck so far.我尝试了很多搜索,但到目前为止没有运气。

Can anyone please advise what am I missing.谁能告诉我我错过了什么。 Following is the code for reference.以下是供参考的代码。

 @IBAction func cameraButtonPressed(_ sender: UIButton) {

    self.openCamera()
  }

  @IBAction func galleryButtonPressed(_ sender: UIButton) {

    self.openGallary()

  }

  override func viewDidLoad() {
    super.viewDidLoad()

    initialLayout()
  }

  override func viewWillAppear(_ animated: Bool) {

    self.lblPreview.isHidden = true
    self.imageTake.isHidden = true
    self.uploadButtonOutlet.isHidden = true
  }

  //
  //MARK: - Internal Methods
  func initialLayout() {

    self.cameraButtonOutlet.layer.cornerRadius = 20
    self.galleryButtonOutlet.layer.cornerRadius = 20
    self.uploadButtonOutlet.layer.cornerRadius = 20

  }


//MARK: - Open the camera
  func openCamera(){

    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){

      imagePicker.sourceType = UIImagePickerController.SourceType.camera
      //If you dont want to edit the photo then you can set allowsEditing to false
      imagePicker.allowsEditing = true
      imagePicker.delegate = self
      imagePicker.cameraCaptureMode = .photo
      imagePicker.cameraDevice = .rear
      self.present(imagePicker, animated: true, completion: nil)
    }
    else{
      let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
      alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
      self.present(alert, animated: true, completion: nil)
    }
  }

  //MARK: - Choose image from camera roll

  func openGallary(){

    imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    imagePicker.allowsEditing = true
    imagePicker.delegate = self
    self.present(imagePicker, animated: true, completion: nil)
  }


extension UploadTimesheetViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate {

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

    if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{

      self.lblPreview.isHidden = false
      self.imageTake.isHidden = false
      self.uploadButtonOutlet.isHidden = false
      DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
        self.imageTake.image = editedImage
        self.imageTake.setNeedsLayout()
      })

      // self.imageTake.image = editedImage
    }

    //Dismiss the UIImagePicker after selection
    picker.dismiss(animated: true, completion: nil)
  }

  func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    picker.isNavigationBarHidden = false
    self.dismiss(animated: true, completion: nil)
  }
}

Update your delegate method like this,像这样更新您的委托方法,

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

    var finalImage:UIImage?

    if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
        finalImage = image
    }else {
        finalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
    }

    self.lblPreview.isHidden = false
    self.imageTake.isHidden = false
    self.uploadButtonOutlet.isHidden = false
    self.imageTake.image = finalImage

    //Dismiss the UIImagePicker after selection
    picker.dismiss(animated: true, completion: nil)

}

Sorry for the delayed response.回复较晚,抱歉。 Just updating the answer so if anyone is having the same issue should know what was the actual problem.只需更新答案,因此如果有人遇到同样的问题,应该知道实际问题是什么。

Once picker is dismissed a viewwillappear will be called where I have hid the image view and hence the reason it was not displaying it for the first time.一旦选择器被关闭,将在我隐藏图像视图的地方调用 viewwillappear,这也是它第一次没有显示它的原因。

If I remove the code from viewwillappear and add that properties code to viewedload then all works fine.如果我从 viewwillappear 中删除代码并将该属性代码添加到viewedload,那么一切正常。

The issue was once the picker is dismissed, viewillapear was called and that was creating the issue.问题是一旦选择器被解雇,viewillapear 就会被调用,这就造成了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM