如何清理iOS应用程序的TMP文件夹

pepwfjgg  于 5个月前  发布在  iOS
关注(0)|答案(3)|浏览(86)

我有一个可以拍照的应用程序(用“允许外部存储文件”标志保存),注意到它的大小增长得太多了。在四处打探后,我发现iOS在tmp目录下创建了一个.LINKS文件夹,每次我保存图片时,它都会被复制到Documents文件夹和.LINKS文件夹中。
当图片被删除时,它在.LINKS中的副本仍保留在那里,占用空间。所以
1.如何清除tmp文件夹?
1.为什么要使用此.LINKS文件夹?
谢谢

cngwdvgl

cngwdvgl1#

找到1的解决方案。:

NSArray* temp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in temp) {
    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}

字符串

inn6fuwd

inn6fuwd2#

事实证明,你不需要删除tmp目录的内容。iOS会定期清理这个目录(特别是当空间变少时)。我发现这篇文章很有帮助:http://kmithi.blogspot.in/2012/08/ios-application-directory-structure.html
.LINKS文件夹被NSImage和相关类用来缓存图像。我有一个应用程序,它会生成大量图像,每次启动该目录都会增长8MB。但在下载一些电影占用空间后,我看到文件夹内容自动清除。

mum43rcc

mum43rcc3#

使用Swift 5解决1

func deleteTemporaryFiles() {
    if let temp = try? FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory()) {
        for file in temp {
            let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(file)
            do {
                try FileManager.default.removeItem(at: filePath)
            } catch {
                print("Error : \(error)")
            }
        }
    }
}

字符串

相关问题