gpt4 book ai didi

ios - 从指定目录中删除所有文件后无法清理内存

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:16 25 4
gpt4 key购买 nike

简介
在我的项目中,我必须删除文件夹及其内容,所以我尝试使用这个已接受的答案 ClickHere

它有效,我认为任务已经结束,但在删除整个文件夹(目录)后,我发现内存仍在分配,但文件不存在。
这是我删除目录(文件夹)的代码).

-(BOOL)removeItem:(NSString*)name{
//name: is directory(folder)'s name
NSString*path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
path = [path stringByAppendingPathComponent:@"Normal"];
path = [path stringByAppendingPathComponent:name];
NSError* err;
NSArray* list = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&err];
if(err){
NSLog(@"\n##CONTENTS OF DIRECTORY ERROR:->\n%@",err.localizedDescription);
}
//if array's count is not zero(0) it means directory has files available.
if(list.count >= 1){
for(NSString* string in list){
[[NSFileManager defaultManager]removeItemAtPath:[path stringByAppendingPathComponent:string] error:&err];
if(err){
NSLog(@"\n##FOLDER IN FOLDER ERROR:->\n%@",err.localizedDescription);
}
}
[[NSFileManager defaultManager]removeItemAtPath:path error:&err];
return YES;
}else{
[[NSFileManager defaultManager]removeItemAtPath:path error:&err];
if (err) {
return NO;
}
return YES;
}
}


提前致谢。

最佳答案

好的,我遇到了问题。在我的例子中,问题是一个临时文件夹(目录)。当我将视频添加到我的应用程序时,iOS 在应用程序的临时文件夹(目录)中创建了一些临时文件。因此,从我的应用程序中删除文件(视频)后,我在设置->常规->使用->管理存储中看到的应用程序大小是临时文件夹的大小。
因此,当您从图库导入照片或视频时,您必须在获取后手动清除临时文件夹。我正在从 UIImagePickerController 的委托(delegate)中清除临时文件夹,如下所示。

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
[self dismissViewControllerAnimated:YES completion:^{
BOOL success = [videoData writeToFile:path atomically:YES];
NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
[RWTAppDelegate clearTmpDirectory];
}];
}


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


swift 3+ 更新

class func clearTmpDirectory(){
let path = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let manager = FileManager.default
let files = try? manager.contentsOfDirectory(atPath: path.path)
files?.forEach { (file) in
let temp = path.appendingPathComponent(file)
try? manager.removeItem(at: temp)
// --- you can use do{} catch{} for error handling ---//
}
}


抱歉我的英语不好。

关于ios - 从指定目录中删除所有文件后无法清理内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32946753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com