简体   繁体   中英

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.

文件目录

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];
        }
}

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"]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM