Swift 4将录制的视频保存到相册

时间:2019-03-22 15:13:32

标签: xcode swift4 avkit

这是我第一次尝试使用AVKit等录制和保存视频,尽管我能够使用以下方法显示和录制视频:

 @IBAction func recordVideo(_ sender: Any) {

        // Check the camera is available
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
            // present the camera
            controller.sourceType = .camera
            controller.mediaTypes = [kUTTypeMovie as String]
            controller.delegate =  self
            present (controller,animated: true, completion: nil)
        }else{
            // No camera available so show alert
            let alert = UIAlertController(title: "Oops!", message: "Can't access the camera!", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Give up", style: .default, handler: nil))
            self.present(alert, animated: true)

            uploadButton.isHidden = true
        }
    }

我无法将录制的视频保存到相册中。当我单步执行代码时,它从“保护”行跳到“其他”块,然后简单地返回。因此由于某种原因,它无法为'mediaType'分配任何内容

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

        dismiss(animated: true, completion: nil)

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

        // Handle a movie capture
        UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, nil, nil)
}

任何帮助将不胜感激。

0 个答案:

没有答案