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

48 views Asked by At

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.

2

There are 2 answers

0
Rob Sanders On BEST ANSWER

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
    }
}
0
Vijay-Apple-Dev.blogspot.com On

Try this dude

-(NSMutableArray*)pullOutAllImagesInDocumentsDirectory{

    NSArray *listOfPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [listOfPaths objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSMutableArray *imageFilePathsCollection = [[NSMutableArray alloc] init];


    for (int imageIndex=0; imageIndex<filePathsArray.count; imageIndex++) {

        NSString *strFilePath = [filePathsArray objectAtIndex:0];

        if ([[strFilePath pathExtension] isEqualToString:@"png"]||
            [[strFilePath pathExtension] isEqualToString:@"PNG"]||
            [[strFilePath pathExtension] isEqualToString:@"jpg"]) {

            [imageFilePathsCollection addObject:[filePathsArray objectAtIndex:imageIndex]];

        }
    }

    return imageFilePathsCollection;

}

NOTE: it will give image url path. in order to get the image, you can use

[yourImageView setImage:[UIImage imageWithContentsOfFile:"Path Of Image"]];