删除在NSDocumentDirectory(Deleting in NSDocumentDirec

2019-09-19 09:40发布

我保存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

谢谢

Answer 1:

试试这个,只需复制该代码,名称为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];
            }
        }
    }


Answer 2:

你看的NSFileManager的方法呢? 也许是这样的呼吁在所有图像的循环。

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


Answer 3:

可以使用类似

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 。 所以,你可以喜欢删除此人。 希望这可以帮助你。



Answer 4:

由于这是一个老问题,现在,也高于答案显示如何通过图像name.What删除,如果我想在一杆,以删除一切从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...
    }
  }

} 


文章来源: Deleting in NSDocumentDirectory