Reputation: 3044
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
Upvotes: 4
Views: 1575
Reputation: 5955
You can do like this :
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] error:NULL];
To set Image :
[imgView setImage:[UIImage imageWithContentsOfFile:"Your complete path"]];
Upvotes: 1
Reputation: 7333
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
Upvotes: 4
Reputation: 8256
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];
}
}
Upvotes: 2