Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all the images from my iPhone App document directory

I have a folder containing a bunch of images. I would like to add the names of all the images to an array. The images are in a folder in my document on the app. See screen shot.

File directory

I know If I would like to get one of these images I would use the following code.

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];
NSString  *filePath = [NSString stringWithFormat:@"%@/POIs/%@", documentsDirectory,image];

Is it posible to select all the files in the folder? If it is would it also be possible to select the names of the files and add them to an array?

Thanks

like image 799
Will Avatar asked Feb 18 '23 20:02

Will


2 Answers

Get the file path of the POS folder:

NSString  *filePath = [NSString stringWithFormat:@"%@/POIs", documentsDirectory];

and then do:

NSArray *files = [fileManager contentsOfDirectoryAtPath:filePath 
                                                  error:nil];

code not tested

like image 123
V-Xtreme Avatar answered Mar 01 '23 22:03

V-Xtreme


Try with below:

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    library = [path objectAtIndex:0];
    fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:library error:nil];
    NSLog(@"fileArray...%@",fileArray);



 for(int i = 0;i<fileArray.count;i++)
    {
        id arrayElement = [fileArray  objectAtIndex:i];
     if ([arrayElement rangeOfString:@".png"].location !=NSNotFound)
        {

        [imagelist addObject:arrayElement];
        arrayToLoad = [[NSMutableArray alloc]initWithArray:imagelist copyItems:TRUE];
        }
}
like image 24
Vishal Avatar answered Mar 01 '23 23:03

Vishal