2

I save in NSDocumentDirectory this way:

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];

I know how to delete all the images in NSDocumentDirectory.

But I was wondering on how to delete all of the images with the name of oneSlotImages.

Thanks

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Bazinga
  • 2,456
  • 33
  • 76
  • Should you be deleting files from the user's document directory without their knowledge and consent? – trojanfoe Jul 12 '12 at 10:40
  • Im saving images. then Im thinking of deleting them and reADDing, updating like. – Bazinga Jul 12 '12 at 10:42
  • Well that seems reasonable, however `[NSData writeToFile:atomically:]` will overwrite the file anyway, so there is no need to delete them first. – trojanfoe Jul 12 '12 at 10:50
  • cause I have an multiple image picker like, a user selects multiple then it shows in preview, however, when removes one check, still same in preview. so Im thinking of deleting them then re-adding. – Bazinga Jul 12 '12 at 10:53

4 Answers4

5

Try this ,just copy this code,your images with name oneSlotImages,will be removed from DocumentDirectory ,its just simple :

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];
            }
        }
    }
Dhruv
  • 2,153
  • 3
  • 21
  • 45
  • Hi how to delete this way in NSCache? – Bazinga Aug 29 '12 at 16:40
  • You can just change `NSDocumentDirectory` to `NSCache`.Did you try this way? – Dhruv Aug 30 '12 at 04:18
  • Yes just replace the single word,as I wrote in above comment.It will work. :) – Dhruv Aug 30 '12 at 05:17
  • and then when I'm loading in NSDocumentDirectory, is the same as in th e NSCache, and I just have to change the path right? Cause i know if I store my images in the NSDocu my app will get rejected. – Bazinga Aug 30 '12 at 06:25
  • Yes exactly,even I had changed it because of same reason.It's perfectly true,go for it. – Dhruv Aug 30 '12 at 06:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16006/discussion-between-born-survivor-and-superman) – Dhruv Aug 30 '12 at 06:33
2

Have you looked at NSFileManager's methods? Maybe something like this called in a loop for all of your images.

[[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
Jonathan King
  • 1,528
  • 14
  • 25
1

Use like,

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

The array zipFiles contains the names of all the files we filtered. Thus by appending the filenames with complete path of document directory with in a loop, you can make the full filepath of all the filtered files in the array. Then you can use a loop and call the method of NSFileManager object like below

[fileManager removeItemAtPath: strGeneratedFilePath error: &err];

which removes the itm at path from the directory.

By this way you can filter out the filenames contains oneSlotImages. So you can prefer to delete this ones. Hope this helps you.

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
0

As this is an old question now and also above answers shows how to delete by image name.What if I want to delete everything from NSDocumentDirectory at one shot, use the below code.

// 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...
    }
  }

} 
Nilesh Kumar
  • 2,141
  • 3
  • 16
  • 20