ios - tmp目录下的文件删除后仍然占用空间ios

标签 ios swift memory tmp

我正在将媒体数据保存到 iPhone 的 tmp 目录,在不再需要它后,我删除了所有文件。但是,我注意到在我的手机设置菜单中,在存储下,每次我保存一个新文件然后删除它时,我的应用程序内存都会增加。我很确定这些文件正在被删除,因为如果我再次尝试删除同一个文件,它会抛出一个异常,表明它不存在。这是我删除 tmp 中所有内容的代码:

let content = try FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())
for path in content {
     let fullPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(path)
     try FileManager.default.removeItem(atPath: fullPath.absoluteString)
}

奇怪的是,有时在不使用应用程序并返回后内存会下降,关键字有时会出现。我是不是做错了什么和/或我还能做些什么来最大限度地减少我的应用程序对 tmp 文件夹的内存占用?

最佳答案

事实证明,将 NSURL 与字符串传递给它是解决方案,现在文件肯定被删除了:

for path in content2 {
    let filePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(path)
    try FileManager.default.removeItem(at: filePath!)
}

关于ios - tmp目录下的文件删除后仍然占用空间ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599320/

相关文章:

html - css 样式不适用于 iOS 中的 <ul> 和 <ol> 的 TextView

amazon-web-services - aws 批处理作业陷入可运行状态,作业定义的内存要求很高

IOS Sinch Calling when Application is offline using APN(Get previously initiated Client)

ios - 核心数据中部分索引的整个字母表

iphone - 如何在 iOS 7 中强制相机拍摄方形照片

ios - 如何在 iOS 后台从 Alamofire 获得稳定的连接

Swift 编译错误位广播要求两个操作数都是指针或都不是

ios - NSTimer 不工作 swift

PHP 使用的内存多于所需的文件

memory - 内存通常比磁盘快多少?