Will
Will

Reputation: 3044

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

Upvotes: 4

Views: 1575

Answers (3)

Venk
Venk

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

V-Xtreme
V-Xtreme

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

Vishal
Vishal

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

Related Questions