NSFileCreationDate = "2017-12-06 06:00:38 +0000"; NSFileSize = 8901; Name = "20171206140036.mp4"; NSFileCreationDate = "2017-12-06 06:00:33 +0000"; NSFileSize = 9332; Name = "20171206140031.mp4"; NSFileCreationDate = "2017-12-05 08:08:43 +0000"; NSFileSize = 26326; Name = "20171205160833.mp4"; NSFileCreationDate = "2017-12-05 07:59:57 +0000"; NSFileSize = 11546; Name = "20171205155953.mp4"; NSFileCreationDate = "2017-12-05 07:54:30 +0000"; NSFileSize = 62277; Name = "20171205155416.mp4"; // 这个获取沙盒相关路径,就是你文件存储的路径 + (NSString *)getVideoPath { NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *getPath = [docPath stringByAppendingPathComponent:@"Videos"]; [[NSFileManager defaultManager] createDirectoryAtPath:getPath withIntermediateDirectories:YES attributes:nil error:nil]; return getPath; 输入文件路径,得到排序好的相关数组,可以设置升序或降序 @param path 文件列表路径 @param isascending 是否升序 @return 排序好的数组 + (NSMutableArray *)visitDirectoryList:(NSString *)path Isascending:(BOOL)isascending { NSArray *fileList = [[NSFileManager defaultManager] subpathsAtPath:path]; // 取得目录下所有文件列表 fileList = [fileList sortedArrayUsingComparator:^(NSString *firFile, NSString *secFile) { // 将文件列表排序 NSString *firPath = [path stringByAppendingPathComponent:firFile]; // 获取前一个文件完整路径 NSString *secPath = [path stringByAppendingPathComponent:secFile]; // 获取后一个文件完整路径 NSDictionary *firFileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:firPath error:nil]; // 获取前一个文件信息 NSDictionary *secFileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:secPath error:nil]; // 获取后一个文件信息 id firData = [firFileInfo objectForKey:NSFileCreationDate]; // 获取前一个文件创建时间 id secData = [secFileInfo objectForKey:NSFileCreationDate]; // 获取后一个文件创建时间 if (isascending) { return [firData compare:secData]; // 升序 } else { return [secData compare:firData]; // 降序 //______________________________________________________________________________________________________ // 将所有文件按照日期分成数组 NSMutableArray *listArray = [NSMutableArray new];//最终数组 NSMutableArray *tempArray = [NSMutableArray new];//每天文件数组 NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd"; for (NSString *fileName in fileList) { NSString *filePath = [path stringByAppendingPathComponent:fileName]; NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; // 获取文件信息 NSMutableDictionary *fileDic = [NSMutableDictionary new]; fileDic[@"Name"] = fileName;//文件名字 fileDic[NSFileSize] = fileInfo[NSFileSize];//文件大小 fileDic[NSFileCreationDate] = fileInfo[NSFileCreationDate];//时间 if (tempArray.count > 0) { // 获取日期进行比较, 按照 XXXX 年 XX 月 XX 日来装数组 NSString *currDate = [format stringFromDate:fileInfo[NSFileCreationDate]]; NSString *lastDate = [format stringFromDate:tempArray.lastObject[NSFileCreationDate]]; if (![currDate isEqualToString:lastDate]) { [listArray addObject:tempArray]; tempArray = [NSMutableArray new]; [tempArray addObject:fileDic]; if (tempArray.count > 0) { // 装载最后一个 array 数组 [listArray addObject:tempArray];