繁体   English   中英

未调用 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不应在viewDidLoad中()方法,但在打开画廊方法。 像这样:

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

        imagePicker.sourceType = .photoLibrary

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM