WKWebView ImagePicker捕获选择图片iOS的事件

时间:2018-06-18 08:14:44

标签: ios uiimagepickercontroller wkwebview

我正在使用WKWebView,我正在点击加载文件'。 WKWebView为我打开UIImagePickerView,在此期间我需要知道 - 选择了什么图像。

1 个答案:

答案 0 :(得分:3)

为我工作。

override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    if let imagePicker = viewControllerToPresent as? UIImagePickerController {
        previousIPDelegate = imagePicker.delegate
        imagePickerController = imagePicker
        imagePickerController?.delegate = self
    }

    super.present(viewControllerToPresent, animated: flag, completion: completion)
}

此后-您将可以使用didFinishPickingMediaWithInfo函数,在其中可以捕获和使用选定的媒体,应将旧的委托返回imagePicker并在旧的委托中调用didFinishPickingMediaWithInfo函数

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    var newInfo = info
    if let pickedImage = newInfo[UIImagePickerControllerOriginalImage] as? UIImage {
        /// DO YOUR STUFF HERE
    }

    picker.delegate = previousIPDelegate
    previousIPDelegate?.imagePickerController!(picker, didFinishPickingMediaWithInfo: newInfo)
}

您应该在DidCancel函数中执行相同的操作

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.delegate = previousIPDelegate
    previousIPDelegate?.imagePickerControllerDidCancel!(picker)
}