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

yiytaume  于 2023-06-25  发布在  iOS
关注(0)|答案(2)|浏览(96)

我正在尝试录制视频,然后保存在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")
        }
    }
}

h22fl7wq

h22fl7wq1#

首先确保将下面的Privacies添加到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)
}
wgmfuz8q

wgmfuz8q2#

我只是发布了一个更新和更详细的版本@HHH答案...使用Xcode 14.3.1swift5

extension MyViewController :  UIImagePickerControllerDelegate, UINavigationControllerDelegate {

  func openCamera() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.sourceType = .camera
            imagePicker.cameraDevice = .front
            imagePicker.cameraFlashMode = .off
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            imagePicker.videoMaximumDuration = 30
            imagePicker.videoQuality = .typeIFrame1280x720
            imagePicker.cameraCaptureMode = .video
            imagePicker.allowsEditing = true
            imagePicker.showsCameraControls = true
            imagePicker.delegate = self
            present(imagePicker, animated: true, completion: nil)
        } else {
            // Camera not available
            print("Camera not avaiable")
        }
    }

// UIImagePickerControllerDelegate method
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let mediaType = info[.mediaType] as? String,
           mediaType == kUTTypeMovie as String,
           let videoURL = info[.mediaURL] as? URL {
            self.videoURL = videoURL
            
            // Process the captured video
            dismiss(animated: true, completion: {
//                self.requestForUploadVideo()
            })
            
        }
    }
    
    // UIImagePickerControllerDelegate method
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }

}

相关问题