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.
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
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
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With