How can I retrieve all the images from NSDocumentsDirectory
? I don't know the image name but I need to store all the images in NSMutableArray
.
How can I get the all images which are stored in NSDocumentsDirectory in iOS?
48 views Asked by Km Ajay Kumar At
2
There are 2 answers
0
On
Try this dude
-(NSMutableArray*)pullOutAllImagesInDocumentsDirectory{
NSArray *listOfPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [listOfPaths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *imageFilePathsCollection = [[NSMutableArray alloc] init];
for (int imageIndex=0; imageIndex<filePathsArray.count; imageIndex++) {
NSString *strFilePath = [filePathsArray objectAtIndex:0];
if ([[strFilePath pathExtension] isEqualToString:@"png"]||
[[strFilePath pathExtension] isEqualToString:@"PNG"]||
[[strFilePath pathExtension] isEqualToString:@"jpg"]) {
[imageFilePathsCollection addObject:[filePathsArray objectAtIndex:imageIndex]];
}
}
return imageFilePathsCollection;
}
NOTE: it will give image url path. in order to get the image, you can use
[yourImageView setImage:[UIImage imageWithContentsOfFile:"Path Of Image"]];
I assuming you mean the Documents Directory specific to your app sandbox. If so you can access that directory using
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Cycle through that directory and look for files that have a .png or .jpg e.t.c. path extension.
i.e: