gpt4 book ai didi

ios - 在 iOS 文档目录中保留数据

转载 作者:行者123 更新时间:2023-11-30 12:34:55 30 4
gpt4 key购买 nike

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

这是保存视频的函数:

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

最佳答案

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

其次,不确定为什么要编写视频并删除它并再次写回,以及为什么使用“尝试”?而不是实际捕获文件写入期间引发的任何异常?

关于ios - 在 iOS 文档目录中保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43006007/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com