Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何在Swift 4+中录制和保存视频;IOS 11+;?_Ios_Swift_Video_Swift4_Ios11 - Fatal编程技术网

我们如何在Swift 4+中录制和保存视频;IOS 11+;?

我们如何在Swift 4+中录制和保存视频;IOS 11+;?,ios,swift,video,swift4,ios11,Ios,Swift,Video,Swift4,Ios11,我正在尝试录制视频,然后将其保存在IOS设备上,我可以录制视频,但我想知道如何将其保存在设备上 import UIKit import AVKit import MobileCoreServices class ViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate { @IBOutlet weak var RecordButton: UIBu

我正在尝试录制视频,然后将其保存在IOS设备上,我可以录制视频,但我想知道如何将其保存在设备上

import UIKit
import AVKit
import MobileCoreServices

class ViewController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {
    @IBOutlet weak var RecordButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func RecordAction(_ sender: UIButton) {

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            print("Camera Available")

            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            imagePicker.allowsEditing = false

            self.present(imagePicker, animated: true, completion: nil)
        } else {
            print("Camera UnAvaialable")
        }
    }
}

首先确保将以下隐私添加到info.plist

Privacy - Photo Library Additions Usage Description
Privacy - Camera Usage Description
Privacy - Microphone Usage Description 
并在ViewDidLoad下添加以下函数

    func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [String : Any]) {
    dismiss(animated: true, completion: nil)

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

    // Handle a movie capture
    UISaveVideoAtPathToSavedPhotosAlbum(
        url.path,
        self,
        #selector(video(_:didFinishSavingWithError:contextInfo:)),
        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: UIAlertActionStyle.cancel, handler: nil))
    present(alert, animated: true, completion: nil)
}

你可以从这里得到参考资料。好运气你想把它保存在设备或应用程序中吗?