Search code examples
iphoneiosobjective-cnsdocumentdirectory

Retrieve multiple images from document directory


I have folder named "2" in document directory. Now in folder "2", I have five images named 0.png, 1.png, 2.png, 3.png and 4.png. I want to retrieve these images and save into an array. I have code but it returns only one image.

int b=2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSLog(@"%@",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d/0.png",b]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
NSLog(@"%@",getImagePath);
imge.image=img;

Solution

  • Just use NSFileManager, something like this:

    [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    

    It will give you an array with all the file paths for all files in the given path.


    This is how you load the images from the array of URLs.

    NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    for (NSURL *url in paths)
    {
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    }