在Swift中将视频保存到本地目录吗?

问题描述

我尝试将给定视频保存在本地,然后需要那些保存的视频才能在我的应用中播放视频。我无法处理保存的视频。这是我的省钱尝试:

func saveVideodocumentDirectory(url : URL){
        let fileManager = FileManager.default
        let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as Nsstring).appendingPathComponent(".MOV")
        do{
            let videoData = try Data(contentsOf: url)
            fileManager.createFile(atPath: paths as String,contents: videoData,attributes: nil)
        }catch{
            //
        }
        
}

这是获取文件的尝试

func getVideo(){
        let fileManager = FileManager.default
        let videoPAth = (self.getDirectoryPath() as Nsstring).appendingPathComponent(".MOV")
        if fileManager.fileExists(atPath: videoPAth){
        print(videoPAth)
        

        play(url: URL(string: videoPAth)!)
        }else{
            print("No Video")
        }
}

这是我的播放视频功能

 func play(url : URL)
        {
        let player = AVPlayer(url: url)
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            present(playerViewController,animated: true)
            {
                playerViewController.player!.play()
            }
}

解决方法

尝试使用$('.decisionList').change(function () { const i = this.selectedIndex; var date = $(this).closest('tr').find('.dates'); if (i == 1 && ($('.invoice').val() < date.val())) { //if InvDate < ManualDate date.val($('.invoice').val()); //then we should return InvDate to our data input } else { date.val(); //if not - return ManualDate } $(this).closest('tr').find('.decisionList')[2]; //make 'None' (index=2) as a selected option }); 代替let attribute = 'z'; let value= 111; // Which one and why or when should i use either one of these? // JS Object let w = { data: function (param) { return param } }; w.constructor.prototype.z = function (param) { console.log(w.data(param)) }; new w[attribute](value); //JS Function let x = function(param){ this.data=param; this.z = function(){ console.log(this.data); }; return this; }; new x(value)[attribute](); // JS Class class y { constructor (param) { this.data = param;}; z() { console.log(this.data)}; }; new y(value)[attribute]();

Filemanager.createFile()

此外,我建议先创建一个文件夹(从answer开始)。

write

然后,您可以像这样保存:

let videoData = try Data(contentsOf: url)
try videoData.write(to: paths,options: .atomic)

这将为您省去extension URL { static func createFolder(folderName: String) -> URL? { let fileManager = FileManager.default // Get document directory for device,this should succeed if let documentDirectory = fileManager.urls(for: .documentDirectory,in: .userDomainMask).first { // Construct a URL with desired folder name let folderURL = documentDirectory.appendingPathComponent(folderName) // If folder URL does not exist,create it if !fileManager.fileExists(atPath: folderURL.path) { do { // Attempt to create folder try fileManager.createDirectory(atPath: folderURL.path,withIntermediateDirectories: true,attributes: nil) } catch { // Creation failed. Print error & return nil print(error.localizedDescription) return nil } } // Folder either exists,or was created. Return URL return folderURL } // Will only be called if document directory not found return nil } } 的麻烦。