viewcontroller 中的所有内容在我从 imagepicker return 之后消失了?

everything in viewcontroller disappeared after I return from imagepicker?

当我尝试在我的应用程序中实现所有 uiimagepickerview 方法时,我的视图控制器(在选择图像之后)没有显示任何内容?它应该有我的文本字段,但它们都是 'disppeared'

这是我的代码

@IBAction func registerUserProfileImageSelectButton(_ sender: UIButton) {
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .photoLibrary
    present(imagePicker, animated: true, completion: nil)
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        registerUserProfileImage.contentMode = .scaleAspectFit
        registerUserProfileImage.image = pickedImage
    }
    dismiss(animated: true, completion: nil)
}


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

我怀疑这可能与我的 dismiss 方法有关,但我不太确定

提前感谢您的帮助!

这对我来说非常有用,你可以试试这个代码:

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    @IBOutlet weak var imagePickerImg: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func imagePicker(_ sender: Any) {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self // you need to set a delegate to your image picker here 
        //to get selected ( edited ) portion
        imagePicker.allowsEditing = true
        present(imagePicker, animated: true, completion: nil)
    }

     public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print(info)

//        let img = info[UIImagePickerControllerOriginalImage] as! UIImage

        // if you keep editing true
        let img = info[UIImagePickerControllerEditedImage] as! UIImage
        imagePickerImg.image = img
        dismiss(animated: true, completion: nil)
    }

    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
        print("Cancel Press")
        dismiss(animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

问题是您试图关闭当前视图控制器,而不是图像选择器控制器。请使用:

imagePicker.dismiss(animated: true, completion: nil)

应该可以!