2

我这样保存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.

谢谢

4

4 回答 4

5

试试这个,只需复制此代码,您的名为 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];
            }
        }
    }
于 2012-07-12T10:59:37.213 回答
2

你看过 NSFileManager 的方法吗?也许这样的事情会在你所有的图像的循环中调用。

[[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
于 2012-07-12T10:45:43.857 回答
1

使用喜欢,

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

该数组zipFiles包含我们过滤的所有文件的名称。因此,通过在循环中附加带有文档目录完整路径的文件名,您可以制作数组中所有过滤文件的完整文件路径。然后您可以使用循环并调用NSFileManager对象的方法,如下所示

[fileManager removeItemAtPath: strGeneratedFilePath error: &err];

从目录中删除路径中的 itm。

通过这种方式,您可以过滤掉包含oneSlotImages. 所以你可以更喜欢删除这些。希望这对您有所帮助。

于 2012-07-12T10:56:32.473 回答
0

由于这是一个老问题,而且上面的答案也显示了如何按图像名称删除。如果我想一次性从 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...
    }
  }

} 
于 2014-08-08T12:30:56.037 回答