Swift - ImagePicker 未执行委托

Swift - ImagePicker not executing delegate

我只想在 ImageView 中显示用 iPhone 相机拍摄的图像。那是项目的第一步,但它不起作用...

这是我的代码:

import UIKit
import MobileCoreServices

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var scanButton: UIButton!
    @IBOutlet weak var textfield: UITextField!

     let imagePicker = UIImagePickerController()


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

        self.imagePicker.modalPresentationStyle = UIModalPresentationStyle.currentContext
        self.imagePicker.delegate = self
        self.imagePicker.mediaTypes = [kUTTypeImage as String]
        self.imagePicker.sourceType = .camera
        self.imagePicker.allowsEditing = true;


    }

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

    //MARK: - Actions

    @IBAction func scanAction(_ sender: UIButton) {

        print("----- scanAction -----");

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


    //MARK: - UIImagePickerControllerDelegate

    private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        print("----- didFinishPickingMediaWithInfo -----");

        var image:UIImage!;
        if let  pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
            image = pickedImage;
        }
        else {
            image = info[UIImagePickerControllerOriginalImage] as! UIImage;
        }

        self.imageView.image = image;


        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("----- imagePickerControllerDidCancel -----");

        dismiss(animated: true, completion: nil)
    }

}

我看到了操作按钮的印刷品,但我从未看到以下按钮:

print("----- didFinishPickingMediaWithInfo -----");

我过去已经imagePicker这样做过,所以我不明白为什么它不起作用。

有人可以帮我吗?

Swift 3 中的信息类型已更改为 [String : Any] 而不是 [String : AnyObject] 而且您还没有添加 _ 作为第一个参数标签,因此请更改您的方法像这样。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("----- imagePickerControllerDidCancel -----");
}