在iOS文档目录中保留数据

时间:2017-03-24 17:47:07

标签: ios swift avfoundation

我有一个将录制的视频保存到文档目录的应用程序,以及Post对象,并从Post对象填充集合视图。但是,在重新启动应用程序时,集合视图为空,因此保存到docs目录的视频不会持续存在(至少我认为这是问题所在。)

这是保存视频的功能:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    dismiss(animated: true, completion: nil)
    if mediaType == kUTTypeMovie {
        var uniqueVideoID = ""
        var videoURL:NSURL? = NSURL()
        var uniqueID = ""

        uniqueID = NSUUID().uuidString

        // Get the path as URL and store the data in myVideoVarData
        videoURL = info[UIImagePickerControllerMediaURL] as? URL as NSURL?
        let myVideoVarData = try! Data(contentsOf: videoURL! as URL)

        // Write data to temp diroctory
        let tempPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let tempDocumentsDirectory: AnyObject = tempPath[0] as AnyObject
        uniqueVideoID = uniqueID  + "TEMPVIDEO.MOV"
        let tempDataPath = tempDocumentsDirectory.appendingPathComponent(uniqueVideoID) as String
        try? myVideoVarData.write(to: URL(fileURLWithPath: tempDataPath), options: [])

        // Get the time value of the video
        let fileURL = URL(fileURLWithPath: tempDataPath)
        let asset = AVAsset(url: fileURL)
        let duration : CMTime = asset.duration

        // Remove the data from the temp Document Diroctory.
        do{
            let fileManager = FileManager.default
            try fileManager.removeItem(atPath: tempDataPath)
        } catch {
            //Do nothing
        }

        // Check to see if video is under the 18500 (:30 seconds)
        if duration.value <= 18500 {

            // Write the data to the Document Directory
            let docPaths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
            let documentsDirectory: AnyObject = docPaths[0] as AnyObject
            uniqueVideoID = uniqueID  + "VIDEO.MOV"
            let docDataPath = documentsDirectory.appendingPathComponent(uniqueVideoID) as String
            try? myVideoVarData.write(to: URL(fileURLWithPath: docDataPath), options: [])
            print("docDataPath under picker ",docDataPath)
            print("Video saved to documents directory")

            //Create a thumbnail image from the video
            let assetImageGenerate = AVAssetImageGenerator(asset: asset)
            assetImageGenerate.appliesPreferredTrackTransform = true
            let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
            if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) {
                //Add thumbnail & video path to Post object
                let video = Post(pathToVideo: URL(fileURLWithPath: docDataPath), thumbnail: UIImage(cgImage: videoImage))
                posts.append(video)
                print("Video saved to Post object")
            }
        }else{
            print("Video not saved")
        }
    }
}

具体来说,这是视频路径和缩略图添加到我的对象的位置:

            //Add thumbnail & video path to Post object
            if let videoImage = try? assetImageGenerate.copyCGImage(at: time, actualTime: nil) {
                let video = Post(pathToVideo: URL(fileURLWithPath: docDataPath), thumbnail: UIImage(cgImage: videoImage))
                posts.append(video)

所以我确实给它了文件目录中视频的路径;我怎样才能确保数据在那里持续存在?

编辑:

enter image description here

1 个答案:

答案 0 :(得分:0)

要验证视频是否正在设备上保存,请将设备连接到Xcode,然后导航到Xcode中的Window-&gt; Devices。然后在左侧选择您的设备,在“已安装的应用”列表中找到您的应用。选择您的应用程序,然后单击列表底部的齿轮图标,然后按“显示容器”。等待几秒钟,您应该会看到应用中的所有文件夹。

其次,不确定为什么要编写视频并将其删除并重新写回来,以及为什么要使用'试试?'而不是实际捕获文件写入期间抛出的任何异常?