如何获取保存在临时目录中的视频的 url
How to get the url for a video saved in temporary directory
我需要将视频文件保存到临时目录并保存对它的引用 URL。目前我已经尝试使用 fileManager 创建一个临时目录,然后创建文件(atPath:tempDirString,内容:vidData,属性:nil)。但是我 dont/am 无法保存对文件的完整引用。
我尝试过的:
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 引用?
非常感谢任何帮助,如果需要可以添加更多信息。谢谢,我
由于您似乎不希望您的文件对用户可见或在应用程序启动之间持续存在,因此 Temporary directory
听起来非常适合您的用例:
var tempVideoFileUrl: URL {
return FileManager.default.temporaryDirectory.appendingPathComponent("my_video_name")
}
func storeVideoToTemporaryFolder(videoData: Data) {
guard !FileManager.default.fileExists(atPath: tempVideoFileUrl.path) else {
return
}
do {
try videoData.write(to: tempVideoFileUrl)
}
catch {
fatalError()
}
}
func loadVideoFromTemporaryFolder() -> Data? {
if let data = try? Data(contentsOf: tempVideoFileUrl) {
return data
}
return nil
}
值得一提的是,系统可能(而且很可能会)在应用程序退出后清除此目录。建议您删除不再需要的任何临时 directories/files。
所以在您的情况下,您可以在完成上传到 Firebase 存储后将其删除:
func deleteVideoFromTemporaryFolder() {
do {
try FileManager.default.removeItem(at: videoFileUrl)
}
catch {
fatalError()
}
}
如果您希望在应用程序启动之间保留您的文件,您可以使用 Application Support directory
。但是由于 Application Support 和 Documents 目录会自动备份,您可能希望通过设置其 URL 的 isExcludedFromBackupKey
键将您的文件从 iCloud 备份中排除:
var applicationSupportVideoFileUrl: URL {
let applicationSupportFolderUrl = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
return applicationSupportFolderUrl.appendingPathComponent("my_video_name")
}
func excludeFromCloudBackup(url: URL) {
var targetUrl = url
var isAlreadyExcludedFromBackup: Bool
do {
let storedRessourceValues = try targetUrl.resourceValues(forKeys: [URLResourceKey.isExcludedFromBackupKey])
isAlreadyExcludedFromBackup = storedRessourceValues.isExcludedFromBackup ?? false
}
catch {
fatalError()
}
guard !isAlreadyExcludedFromBackup else {
return
}
var ressourceValues = URLResourceValues()
ressourceValues.isExcludedFromBackup = true
do {
try targetUrl.setResourceValues(ressourceValues)
}
catch {
fatalError()
}
}
编辑: 要从您的 PHAsset 获取数据,这应该可行:
import Photos
func loadVideoData(phAsset: PHAsset, completion: @escaping (Data?)->()) {
guard phAsset.mediaType == .video else {
return completion(nil)
}
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true
options.deliveryMode = .highQualityFormat
PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: options) { (avAsset, _, _) in
guard let avUrlAsset = avAsset as? AVURLAsset else {
return
}
var videoData: Data?
do {
videoData = try Data(contentsOf: avUrlAsset.url)
} catch {
fatalError()
}
DispatchQueue.main.async {
completion(videoData)
}
}
}
然后只需调用此方法并将您的视频存储在临时文件夹中:
loadVideoData(phAsset: yourPhAsset) { [weak self] videoData in
guard let strongSelf = self else { return }
guard let videoData = videoData else {
return
}
strongSelf.storeVideoToTemporaryFolder(videoData: videoData)
}
我需要将视频文件保存到临时目录并保存对它的引用 URL。目前我已经尝试使用 fileManager 创建一个临时目录,然后创建文件(atPath:tempDirString,内容:vidData,属性:nil)。但是我 dont/am 无法保存对文件的完整引用。
我尝试过的:
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 引用?
非常感谢任何帮助,如果需要可以添加更多信息。谢谢,我
由于您似乎不希望您的文件对用户可见或在应用程序启动之间持续存在,因此 Temporary directory
听起来非常适合您的用例:
var tempVideoFileUrl: URL {
return FileManager.default.temporaryDirectory.appendingPathComponent("my_video_name")
}
func storeVideoToTemporaryFolder(videoData: Data) {
guard !FileManager.default.fileExists(atPath: tempVideoFileUrl.path) else {
return
}
do {
try videoData.write(to: tempVideoFileUrl)
}
catch {
fatalError()
}
}
func loadVideoFromTemporaryFolder() -> Data? {
if let data = try? Data(contentsOf: tempVideoFileUrl) {
return data
}
return nil
}
值得一提的是,系统可能(而且很可能会)在应用程序退出后清除此目录。建议您删除不再需要的任何临时 directories/files。
所以在您的情况下,您可以在完成上传到 Firebase 存储后将其删除:
func deleteVideoFromTemporaryFolder() {
do {
try FileManager.default.removeItem(at: videoFileUrl)
}
catch {
fatalError()
}
}
如果您希望在应用程序启动之间保留您的文件,您可以使用 Application Support directory
。但是由于 Application Support 和 Documents 目录会自动备份,您可能希望通过设置其 URL 的 isExcludedFromBackupKey
键将您的文件从 iCloud 备份中排除:
var applicationSupportVideoFileUrl: URL {
let applicationSupportFolderUrl = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
return applicationSupportFolderUrl.appendingPathComponent("my_video_name")
}
func excludeFromCloudBackup(url: URL) {
var targetUrl = url
var isAlreadyExcludedFromBackup: Bool
do {
let storedRessourceValues = try targetUrl.resourceValues(forKeys: [URLResourceKey.isExcludedFromBackupKey])
isAlreadyExcludedFromBackup = storedRessourceValues.isExcludedFromBackup ?? false
}
catch {
fatalError()
}
guard !isAlreadyExcludedFromBackup else {
return
}
var ressourceValues = URLResourceValues()
ressourceValues.isExcludedFromBackup = true
do {
try targetUrl.setResourceValues(ressourceValues)
}
catch {
fatalError()
}
}
编辑: 要从您的 PHAsset 获取数据,这应该可行:
import Photos
func loadVideoData(phAsset: PHAsset, completion: @escaping (Data?)->()) {
guard phAsset.mediaType == .video else {
return completion(nil)
}
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true
options.deliveryMode = .highQualityFormat
PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: options) { (avAsset, _, _) in
guard let avUrlAsset = avAsset as? AVURLAsset else {
return
}
var videoData: Data?
do {
videoData = try Data(contentsOf: avUrlAsset.url)
} catch {
fatalError()
}
DispatchQueue.main.async {
completion(videoData)
}
}
}
然后只需调用此方法并将您的视频存储在临时文件夹中:
loadVideoData(phAsset: yourPhAsset) { [weak self] videoData in
guard let strongSelf = self else { return }
guard let videoData = videoData else {
return
}
strongSelf.storeVideoToTemporaryFolder(videoData: videoData)
}