Swift中的相机和库动作

时间:2018-09-01 12:37:44

标签: ios swift delegates

我在Swift编码方面非常陌生,并且相机和库的使用存在问题。我有这部分代码:

    @IBAction func camera(_ sender: Any) {

    if !UIImagePickerController.isSourceTypeAvailable(.camera) {
        return
    }

    let cameraPicker = UIImagePickerController()
    cameraPicker.delegate = self
    cameraPicker.sourceType = .camera
    cameraPicker.allowsEditing = false

    present(cameraPicker, animated: true)
}

@IBAction func openLibrary(_ sender: Any) {
    let picker = UIImagePickerController()
    picker.allowsEditing = false
    picker.delegate = self
    picker.sourceType = .photoLibrary
    present(picker, animated: true)
}

}

并在cameraPicker.delegate = self和picker.delegate = self中引发如下错误:

  

无法将类型为“ ViewController”的值分配为类型为“((UIImagePickerControllerDelegate&UINavigationControllerDelegate)”吗?”

我真的不知道该如何解决。如果有人可以帮助我,我将非常感激,在此先感谢您!

1 个答案:

答案 0 :(得分:1)

只需添加如下的代表

@Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated( view, savedInstanceState );
        Thread thread=new Thread(  ) {
            @Override
            public void run() {
                try {
                    sleep( 3000 );
                    Navigation.findNavController(view).navigate( R.id.action_splashScreen_to_mainFragment );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        thread.start();
    }