在NSDocumentDirectory中删除

我保存在NSDocumentDirectory这种方式:

 NSLog(@"%@", [info objectAtIndex:i]); NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES ); NSString *documentsDir = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; //----resize the images image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)]; NSData *imageData = UIImagePNGRepresentation(image); [imageData writeToFile:savedImagePath atomically:YES]; 

我知道如何删除NSDocumentDirectory所有图像。

但我想知道如何删除oneSlotImages名称的所有图像。

谢谢

试试这个,只要复制这个代码,你的名字为oneSlotImages的图像将被从DocumentDirectory中删除,它只是简单的:

 NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] error:NULL]; if([directoryContents count] > 0) { for (NSString *path in directoryContents) { NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:path]; NSRange r =[fullPath rangeOfString:@"oneSlotImages"]; if (r.location != NSNotFound || r.length == [@"oneSlotImages" length]) { [[NSFileManager defaultManager] removeItemAtPath:fullPath error:nil]; } } } 

你看过NSFileManager的方法吗? 也许像这样的东西在所有的图像循环调用。

 [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL]; 

使用像,

 NSArray *dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:strDirectoryPath error:nil]; NSArray *zipFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", @"oneSlotImages"]]; 

数组zipFiles包含我们过滤的所有文件的名称。 因此,通过在循环中附加文件目录的完整path的文件名,可以制作数组中所有过滤文件的完整文件path。 然后你可以使用一个循环,像下面这样调用NSFileManager对象的方法

 [fileManager removeItemAtPath: strGeneratedFilePath error: &err]; 

从目录中删除它的path。

通过这种方式,你可以过滤掉包含oneSlotImages的文件名。 所以你可以更喜欢删除这些。 希望这可以帮助你。

由于这是一个古老的问题,上面的答案也显示了如何通过图像名称删除。如果我想一次从NSDocumentDirectory删除所有内容,请使用下面的代码。

 // Path to the Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); if ([paths count] > 0) { NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; // Print out the path to verify we are in the right place NSString *directory = [paths objectAtIndex:0]; NSLog(@"Directory: %@", directory); // For each file in the directory, create full path and delete the file for (NSString *file in [fileManager contentsOfDirectoryAtPath:directory error:&error]) { NSString *filePath = [directory stringByAppendingPathComponent:file]; NSLog(@"File : %@", filePath); BOOL fileDeleted = [fileManager removeItemAtPath:filePath error:&error]; if (fileDeleted != YES || error != nil) { // Deal with the error... } } }