未调用 UIImagePickerController 委托

UIImagePickerController delegate is not being called

使用下面的代码打开相机但无法调用选择器委托方法。我没有收到任何错误消息。

import Foundation
import UIKit
import MobileCoreServices

class RecVidController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        RecVidController.startRecord(delegate: self, sourceType: .camera)
    }

    static func startRecord(delegate: UIViewController & UINavigationControllerDelegate & UIImagePickerControllerDelegate, sourceType: UIImagePickerController.SourceType) {
        guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { return }

        let mediaUI = UIImagePickerController()
        mediaUI.sourceType = sourceType
        mediaUI.mediaTypes = [kUTTypeMovie as String]
        mediaUI.allowsEditing = true
        mediaUI.delegate = delegate
        delegate.present(mediaUI, animated: true, completion: nil)
    }

    @objc func video(_ videoPath: String, didFinishSavingWithError error: Error?, contextInfo info: AnyObject) {
        let title = (error == nil) ? "Success" : "Error"
        let message = (error == nil) ? "Video was saved" : "Video failed to save"

        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }

}

// MARK: - UIImagePickerControllerDelegate

extension RecVidController: UIImagePickerControllerDelegate {
    private func imagePickerController(_ picker: UIImagePickerController,
                                       didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        dismiss(animated: true, completion: nil)

        guard let mediaType = info[UIImagePickerController.InfoKey.mediaType] as? String,
            mediaType == (kUTTypeMovie as String),
            let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL,
            UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.path)
            else { return }

        // Handle a movie capture
        UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, #selector(video(_:didFinishSavingWithError:contextInfo:)), nil)
    }

}

// MARK: - UINavigationControllerDelegate

extension RecVidController: UINavigationControllerDelegate {
}

问题是这个声明:

private func imagePickerController(
    _ picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

您已声明此方法 private,因此 Objective-C 无法知道它的存在。因此,它不能调用它。

基本上,Cocoa 会查看此方法是否已实现,发现它不是(因为您已将其隐藏),然后放弃。没有明显的惩罚,因为此委托方法是可选的,如果未实现,Cocoa 会在用户完成选择器后为您关闭它。

所以只需删除 private 就可以了。这将委托方法公开给 Objective-C,因此它将被调用。

(你不必说 @objc 将它暴露给 Objective-C,如果这是你自己的函数,你会这样做,因为你已经声明我们采用 UIImagePickerControllerDelegate ,这是一个 Objective-C 协议。)

当程序未调用委托方法时,我遇到了类似的问题。

imagePicker.delegate = self 在 vi​​ewDidLoad() 方法中应该是 NOT 但在打开画廊的方法中。像这样:

func openGallery()
    {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self

        imagePicker.sourceType = .photoLibrary

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