0

How can you remove all the files that are saved using the NSFileManager ? I found something like the code below, but you need to give the filePath and since I need to delete all the images that I've saved, I don't know all the image paths/names?

[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
4

2 回答 2

0

首先从该特定文件夹中获取所有项目:

NSString  *filePath = [NSString stringWithFormat:@"your folder path where you have saved those images"];
NSArray *files = [fileManager contentsOfDirectoryAtPath:filePath 
                                                  error:nil];

这将是刚刚遍历数组的文件数组。并使用:

  for(NSString *filename in files)
   {
    NSString  *filePath = [NSString stringWithFormat:@"filepath/%@",filename];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
   }

这应该可以解决问题。

于 2013-03-22T12:04:46.600 回答
0

您必须将图像保存到任何文件夹,例如 Documents Directory、Library、Caches 或 Temp 文件夹。您需要使用以下代码获取保存图像的任何目录中的所有图像。

-(NSArray*)getImagesFromDirectory:(NSString*)imageDirectoryPath{

   NSFileManager *fileManager = [[NSFileManager alloc] init];
   NSArray *files = [fileManager contentsOfDirectoryAtPath:imageDirectoryPath 
                                          error:nil];
   return files;
}

然后循环删除路径中的图像

 NSArray *images=[self getImagesFromDirectory:@"DIRECTORYPATH"];
 NSFileManager * fileManager = [[NSFileManager alloc] init];
 for(NSString *imagePath in images){
     [fileManager removeItemAtPath:imagePath];
 }
于 2013-03-22T12:05:07.400 回答