Search code examples
iosiphonensdocumentdirectory

How can I get the all images which are stored in NSDocumentsDirectory in iOS?


How can I retrieve all the images from NSDocumentsDirectory? I don't know the image name but I need to store all the images in NSMutableArray.


Solution

  • I assuming you mean the Documents Directory specific to your app sandbox. If so you can access that directory using [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    Cycle through that directory and look for files that have a .png or .jpg e.t.c. path extension.

    i.e:

    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSArray *array = [[NSFileManager defaultManager] contentsAtPath:string];
    for (NSString *doc in array) {
        NSLog(@"%@", doc);
        if ([doc.pathExtension isEqualToString:@"jpg"]) {
        // Is jpg image obviously you want to search for different formats also
        }
    }