我经常搜索但未找到任何解决方案。我正在开发创建视频并保存在本地目录的应用程序,并在应用程序的前面屏幕上显示这些已保存的视频。但我想在屏幕上只显示6个最新保存的视频。如何从目录中获取最新的视频路径。请帮忙。
这是我用来获取所有视频文件的代码
[[NSFileManager defaultManager] fileExistsAtPath:DocumentPath
isDirectory:&isDir];
if ( isDir ) {
NSMutableArray *contentItemArray = [[NSMutableArray alloc] init];
NSArray *contentOfDirectory =
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:finalDirectory
error:NULL];
for (int i = 0; i<[contentOfDirectory count]; i++) {
NSString *fileName = [contentOfDirectory objectAtIndex:i];
if([fileName.pathExtension isEqualToString:@"mov"])
{
[contentItemArray addObject:fileName];
}
} }
答案 0 :(得分:1)
也许您可以尝试搜索创建日期并选择已创建的最后6个,如下所示:
[[NSFileManager defaultManager] fileExistsAtPath:DocumentPath
isDirectory:&isDir];
if ( isDir ) {
NSMutableArray *contentItemArray = [[NSMutableArray alloc] init];
NSArray *contentOfDirectory =
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:finalDirectory
error:NULL];
for (int i = 0; i<[contentOfDirectory count]; i++) {
NSString *fileName = [contentOfDirectory objectAtIndex:i];
if([fileName.pathExtension isEqualToString:@"mov"])
{
[contentItemArray addObject:fileName];
NSURL *fileUrl = [NSURL URLWithString:DocumentPath];
NSDate *fileDate;
[fileName getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error];
if (!error)
{
//here you should be able to read valid date from fileDate variable
}
}
} }*
希望这可以帮到你,否则我们会找到别的事情。
答案 1 :(得分:0)
-(NSArray *)getLatestFile:(NSArray *)completedMilestoneArray {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSArray *sortedKeys = [completedMilestoneArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *s1 = [[obj1 objectAtIndex:1] stringByDeletingPathExtension];
NSString *s2 = [[obj2 objectAtIndex:1] stringByDeletingPathExtension];
NSDate *d1 = [NSDate dateWithTimeIntervalSince1970:(int)s1];
//NSDate *d1 = [dateFormatter dateFromString:s1];
NSDate *d2 = [NSDate dateWithTimeIntervalSince1970:(int)s2];
if ([d2 compare:d1] == NSOrderedAscending)
return (NSComparisonResult)NSOrderedAscending;
if ([d2 compare:d1] == NSOrderedDescending)
return (NSComparisonResult)NSOrderedDescending;
return (NSComparisonResult)NSOrderedSame;
}];
return sortedKeys; }