我有一个应用程序,允许用户录制多个视频,然后将它们从数据库中发布查看。在他们发布视频后,我希望应用程序从文档目录中删除视频,以节省手机存储空间。我正在尝试这个,但当我检查我的手机的存储没有任何更新。下面是我正在做的事情
这就是我要写视频的地方:
func videoFileLocation() -> String {
let uniqueID = NSUUID().uuidString
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let outputPath = "\(documentsPath)/\(uniqueID).mov"
return outputPath
}下面是我删除它们的方法:
func clearDirectory() {
do {
data = try context.fetch(VideoPath.fetchRequest())
for each in data {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let path = "\(documentsPath)/\(each.fileLocations!)"
let url = URL(fileURLWithPath: path)
do {
try FileManager.default.removeItem(at: url)
print("Successfully Cleared")
} catch {
print("There was a problem removing the file")
}
}成功清除的方法会打印出来,但我在手机的存储空间中看不到任何变化。我做错了什么?
发布于 2017-08-04 09:57:11
替换
let path = "\(documentsPath)/\(each.fileLocations!)"使用下面的代码并尝试。
var path: String = nil
if let fileLocationPath = each.fileLocations as? String {
path = documentsPath.appendingPathComponent(fileLocationPath)
}https://stackoverflow.com/questions/45497046
复制相似问题