在NSDocumentDirectory中删除

时间:2012-07-12 10:38:41

标签: iphone ios xcode ipad

我以这种方式保存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 个答案:

答案 0 :(得分: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];
            }
        }
    }

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

使用like,

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的文件名。所以你可以选择删除这些。希望这会对你有所帮助。

答案 3 :(得分: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...
    }
  }

}