This question already has an answer here:
-
How to sort files by modified date in iOS
5 answers
How to display images by sorting date modified type from gallery. I can get data from folder using
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
Also how to compare uiimage by date modified.
Thanks in Advance....
Simplest way:
- (void)images
{
//---Get directory from app folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *originalImage = [[NSMutableArray alloc]init];
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
[originalImage addObject:image];
}
NSLog(@"\n\n\n Original images %@",originalImage);
//---------sorting image by date modified
NSArray* filelist_date_sorted;
filelist_date_sorted = [imageFilenames sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDictionary* first_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj1] error:nil];
NSDate *first = [first_properties objectForKey:NSFileModificationDate];
NSDictionary *second_properties = [[NSFileManager defaultManager] attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, obj2] error:nil];
NSDate *second = [second_properties objectForKey:NSFileModificationDate];
return [second compare:first];
}];
NSLog(@"\n\n\n date images %@",filelist_date_sorted);
NSMutableArray *sortedImage = [[NSMutableArray alloc]init];
//--- Store sorted images in array
for (int i = 1; i < [imageFilenames count]; i++)
{
NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[filelist_date_sorted objectAtIndex:i] ];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
if(!(image==nil))
{
[sortedImage addObject:image];
}
}
NSLog(@"\n\n\nsorted images %@",sortedImage);
}
For that you have to used ALAssetsLibrary to enumerate all images from gallery.
From that you have fetch attributes of each image:
- image file name.
- image asset url
and your answer is here:
You can retrieve the creation date of the asset using the ALAssetPropertyDate property:
ALAsset* asset;
//do anthing
NSDate* date = [asset valueForProperty:ALAssetPropertyDate];
may be it will help you.
You can get attributes of image using this code
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:@"path/to/my/file" error:nil];
You can get modified date using this code
NSDate *date = [attributes fileModificationDate];
Create NSMutableDictionary and and put modified date as key and path as value.
NSMutableDictionary *imagesDict = [[NSMutableDictionary alloc] init];
for(NSString *path in paths)
{
NSDictionary *attributes = [[NSFileManager defaultManager] ;
NSDate *date = [attributes fileModificationDate];
imagesDict[date] = path; // key - date , value - path
}
Then you can sort keys of imagesDict
NSArray *sortedDates = [[imagesDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
and then you can make cycle for sortedDates items and get sorted by date pathes for your images
for(NSDate *newDate in sortedDates)
{
NSString newPath = imagesDict[newDate];
}
Another solution:
-(NSArray *)orderedFileListByModificationDate{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [paths objectAtIndex:0];
dirPath=[dirPath stringByAppendingString:@"/imgs"];
NSFileManager *fileManager=[[NSFileManager alloc] init];
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];
int filesCount;
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:NULL];
int dirFilesCount=(int)[directoryContent count];
NSMutableArray *fileObjects=[NSMutableArray array];
for (filesCount = 0; filesCount < dirFilesCount; filesCount++)
{
NSString *fileName=[directoryContent objectAtIndex:filesCount];
NSString *filePath =[NSString stringWithFormat:@"%@/%@",dirPath,fileName];
NSDictionary *fileAttributes=[fileManager attributesOfItemAtPath:filePath error:nil];
NSDate *dateModiftication=[fileAttributes valueForKey:@"NSFileModificationDate"];
NSMutableDictionary *datesAndFilesDict=[[NSMutableDictionary alloc] initWithCapacity:2];
[datesAndFilesDict setValue:fileName forKey:@"fileName"];
[datesAndFilesDict setValue:dateModiftication forKey:@"date"];
[fileObjects addObject:datesAndFilesDict];
}
NSLog(@"%@",fileObjects);
NSArray *newOrderedFileList = [fileObjects sortedArrayUsingComparator:
^(id obj1, id obj2) {
NSDate *dateOne=[(NSDictionary*)obj1 valueForKey:@"date"];
NSDate *dateTwo=[(NSDictionary*)obj2 valueForKey:@"date"];
NSComparisonResult result=[dateOne compare:dateTwo];
return result;
}];
NSLog(@"%@",newOrderedFileList);
//Now you've an array of dictionaries where you can pick the filename with the "fileName" key.
return newOrderedFileList;
}