Swift - ImagePicker不执行委托

时间:2016-10-04 10:28:57

标签: ios swift uiimageview uiimagepickercontroller swift3

我只想在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,所以我不明白为什么它不起作用。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

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

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