【发布时间】:2020-05-31 04:22:42
【问题描述】:
我需要将视频文件保存到临时目录并保存对它的引用 URL。目前我已经尝试使用 fileManager 创建一个临时目录,然后 createFile(atPath: tempDirString, contents: vidData, attributes: nil)。但我不能/无法保存对文件的完整引用。
我尝试过的:
PHCachingImageManager().requestAVAsset(forVideo: (cell?.assetPH)!, options: nil) { (avAsset, _, _) in
if let avAsset = avAsset {
print(avAsset, " the avasset?")
// vidAVAsset = avAsset
let avPlayerItem = AVPlayerItem(asset: avAsset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
print(avPlayer, "<-- good?")
let finalURL = self.urlOfCurrentlyPlayingInPlayer(player: avPlayer)
print(finalURL, "<-- finalURL YEAH EYAHY YEAH?!?!?")
// newUserTakenVideo.videoURL = finalURL
let url = self.addVidToTempURL(vidURL: finalURL!)
newUserTakenVideo.videoURL = url
// let newUserTakenVideo = SelectedMedia(videoURL: finalURL, phAsset: (cell?.assetPH)!, thumbnailImg: (cell?.imageView.image)!, uniqueCellID: indexPath)
// GlobalSharedData.shared.arrayOfCurrentCreateMedia.append(newUserTakenVideo)
} else {
print("did noot work")
}
}
这是调用的函数:
func addVidToTempURL(vidURL: URL) -> URL? {
let fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory
let tempDirString = tempDir.path
do {
print( "tempDir: \(tempDir)" )
print( "tempDirString: \(tempDirString)" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
do {
try fileManager.createDirectory( at: tempDir, withIntermediateDirectories: true, attributes: nil )
print( "tempDir created" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
let vidData = try Data(contentsOf: vidURL)
fileManager.createFile(atPath: tempDirString, contents: vidData, attributes: nil)
print(tempDirString, " the stringfsdsda")
// fileManager.urls(for: fileManager.temporaryDirectory, in: fileManager.)
let url = URL(string: tempDirString)
return url
} else {
print( "tempDir STILL DOES NOT exist" )
return nil
}
} catch {
print( "tempDir NOT created" )
return nil
}
} else {
print( "tempDir DOES NOT exist" )
do {
try fileManager.createDirectory( at: tempDir, withIntermediateDirectories: true, attributes: nil )
print( "tempDir created" )
if fileManager.fileExists(atPath: tempDirString ) {
print( "tempDir exists" )
let vidData = try Data(contentsOf: vidURL)
fileManager.createFile(atPath: tempDirString, contents: vidData, attributes: nil)
print(tempDirString, " the fsdfdsdfsdfsfsd")
let url = URL(string: tempDirString)
return url
} else {
print( "tempDir STILL DOES NOT exist" )
return nil
}
} catch {
print( "tempDir NOT created" )
return nil
}
}
}
}
如何获取此文件位置的 URL 引用?
感谢您的帮助,如果需要,可以添加更多信息。谢谢,我
【问题讨论】:
-
顾名思义,它是临时的,因此一旦您的方法完成,系统可能(并且将)删除它。因此,您有责任在返回 url 之前将您的项目复制/移动到常规文件夹。不要从 tmp 位置返回 url。你不会在那里找到文件了
-
那么我怎样才能将这个视频文件存储在某个地方以便以后检索呢?它是一个 PHAsset,来自用户的 cam roll,因此除非保存,否则它似乎会消失。 @LeoDabus
-
您有很多选择,这完全取决于您是否希望用户可以访问您的文件developer.apple.com/library/archive/documentation/…
-
@LeoDabus “取决于您是否希望用户可以访问您的文件”不。我会将视频上传到 firebase
-
您可以使用应用支持目录stackoverflow.com/a/34701970/2303865
标签: ios swift file-management