我正在开发一个使用iPhone摄像头的应用程序,经过几次测试,我意识到它将所有捕获的视频存储在应用程序的tmp目录中。捕获不会消失,即使手机重新启动。有没有什么方法可以删除所有这些捕获或有没有什么方法可以轻松地清理所有缓存和临时文件?
dy2hfwbg1#
是的。这个方法很有效:
+ (void)clearTmpDirectory { NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]; for (NSString *file in tmpDirectory) { [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL]; } }
ej83mcc02#
Swift 3版本作为扩展:
extension FileManager { func clearTmpDirectory() { do { let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory()) try tmpDirectory.forEach {[unowned self] file in let path = String.init(format: "%@%@", NSTemporaryDirectory(), file) try self.removeItem(atPath: path) } } catch { print(error) } } }
用法示例:
FileManager.default.clearTmpDirectory()
感谢Max Maier,Swift 2版本:
func clearTmpDirectory() { do { let tmpDirectory = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(NSTemporaryDirectory()) try tmpDirectory.forEach { file in let path = String.init(format: "%@%@", NSTemporaryDirectory(), file) try NSFileManager.defaultManager().removeItemAtPath(path) } } catch { print(error) } }
0h4hbjxa3#
Swift 4酒店
可能的实现之一
extension FileManager { func clearTmpDirectory() { do { let tmpDirURL = FileManager.default.temporaryDirectory let tmpDirectory = try contentsOfDirectory(atPath: tmpDirURL.path) try tmpDirectory.forEach { file in let fileUrl = tmpDirURL.appendingPathComponent(file) try removeItem(atPath: fileUrl.path) } } catch { //catch the error somehow } } }
scyqe7ek4#
尝试此代码删除NSTemporaryDirectory文件
-(void)deleteTempData { NSString *tmpDirectory = NSTemporaryDirectory(); NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error]; for (NSString *file in cacheFiles) { error = nil; [fileManager removeItemAtPath:[tmpDirectory stringByAppendingPathComponent:file] error:&error]; } }
并检查数据删除或不写入didFinishLaunchingWithOptions中的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window makeKeyAndVisible]; NSString *tmpDirectory = NSTemporaryDirectory(); NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error]; NSLog(@"TempFile Count ::%lu",(unsigned long)cacheFiles.count); return YES; }
pzfprimi5#
感谢Max Maier和Roman Barzyczak更新到Swift 3,使用URL而不是字符串。
func clearTmpDir(){ var removed: Int = 0 do { let tmpDirURL = URL(string: NSTemporaryDirectory())! let tmpFiles = try FileManager.default.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) print("\(tmpFiles.count) temporary files found") for url in tmpFiles { removed += 1 try FileManager.default.removeItem(at: url) } print("\(removed) temporary files removed") } catch { print(error) print("\(removed) temporary files removed") } }
kupeojn66#
这适用于越狱的iPad,但我认为这也应该适用于非越狱设备。
-(void) clearCache { for(int i=0; i< 100;i++) { NSLog(@"warning CLEAR CACHE--------"); } NSFileManager *fileManager = [NSFileManager defaultManager]; NSError * error; NSArray * cacheFiles = [fileManager contentsOfDirectoryAtPath:NSTemporaryDirectory() error:&error]; for(NSString * file in cacheFiles) { error=nil; NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:file ]; NSLog(@"filePath to remove = %@",filePath); BOOL removed =[fileManager removeItemAtPath:filePath error:&error]; if(removed ==NO) { NSLog(@"removed ==NO"); } if(error) { NSLog(@"%@", [error description]); } } }
ih99xse17#
我知道我迟到了,但我想放弃我的实现,它也可以直接在URL上工作:
let fileManager = FileManager.default let temporaryDirectory = fileManager.temporaryDirectory try? fileManager .contentsOfDirectory(at: temporaryDirectory, includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants) .forEach { file in try? fileManager.removeItem(atPath: file.path) }
fykwrbwg8#
// // FileManager+removeContentsOfTemporaryDirectory.swift // // Created by _ _ on _._.202_. // Copyright © 202_ _ _. All rights reserved. // import Foundation public extension FileManager { /// Perform this method on a background thread. /// Returns `true` if : /// * all temporary folder files have been deleted. /// * the temporary folder is empty. /// Returns `false` if : /// * some temporary folder files have not been deleted. /// Error handling: /// * Throws `contentsOfDirectory` directory access error. /// * Ignores single file `removeItem` errors. /// @discardableResult func removeContentsOfTemporaryDirectory() throws -> Bool { if Thread.isMainThread { let mainThreadWarningMessage = "\(#file) - \(#function) executed on main thread. Do not block the main thread." assertionFailure(mainThreadWarningMessage) } do { let tmpDirURL = FileManager.default.temporaryDirectory let tmpDirectoryContent = try contentsOfDirectory(atPath: tmpDirURL.path) guard tmpDirectoryContent.count != 0 else { return true } for tmpFilePath in tmpDirectoryContent { let trashFileURL = tmpDirURL.appendingPathComponent(tmpFilePath) try removeItem(atPath: trashFileURL.path) } let tmpDirectoryContentAfterDeletion = try contentsOfDirectory(atPath: tmpDirURL.path) return tmpDirectoryContentAfterDeletion.count == 0 } catch let directoryAccessError { throw directoryAccessError } } }
zujrkrfu9#
使用此专用代码删除所有tmp文件:
let directory = FileManager.default.temporaryDirectory try contentsOfDirectory(at: directory, includingPropertiesForKeys: []).forEach(removeItem)
如果需要,可以在执行forEach之前过滤内容
forEach
9条答案
按热度按时间dy2hfwbg1#
是的。这个方法很有效:
ej83mcc02#
Swift 3版本作为扩展:
用法示例:
感谢Max Maier,Swift 2版本:
0h4hbjxa3#
Swift 4酒店
可能的实现之一
scyqe7ek4#
尝试此代码删除NSTemporaryDirectory文件
并检查数据删除或不写入didFinishLaunchingWithOptions中的代码
pzfprimi5#
感谢Max Maier和Roman Barzyczak更新到Swift 3,使用URL而不是字符串。
Swift 3
kupeojn66#
这适用于越狱的iPad,但我认为这也应该适用于非越狱设备。
ih99xse17#
我知道我迟到了,但我想放弃我的实现,它也可以直接在URL上工作:
fykwrbwg8#
zujrkrfu9#
使用此专用代码删除所有tmp文件:
如果需要,可以在执行
forEach
之前过滤内容