【问题标题】:getting latest saved file from Directory从目录中获取最新保存的文件
【发布时间】:2014-06-09 08:39:18
【问题描述】:

我搜索了很多,但没有找到任何解决方案。我正在开发创建视频并保存在本地目录并在应用程序的前屏幕上显示这些保存的视频的应用程序。但我只想在屏幕上显示 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];
        }
   } }

【问题讨论】:

  • 这个SO question 可以帮助你,它展示了如何检索创建日期。

标签: ios objective-c nsfilemanager


【解决方案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
            }
        }
    } }*

希望这对你有帮助,否则我们会找别的事情做。

【讨论】:

  • 对不起@Norolimba:我退出了新手,对我来说还不清楚。那么请问我如何在我的情况下使用它?
  • 我已经编辑过了,希望对您有所帮助,如果没有我们可以尝试其他方法,但它应该适用于这个
【解决方案2】:

是的,我知道了。使用当前日期保存视频并使用此方法。这将返回排序项目数组。

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

【讨论】:

  • 另一个答案更好,因为 creation date 是文件 metadata 的一部分(因此您不需要将其包含在文件名中除非您试图避免名称冲突)。
  • @trojanfoe:是的,我想要像你一样,但我对目标 C 不熟悉,其他答案对我来说不清楚,你想告诉我如何使用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 2021-10-23
  • 2014-08-27
  • 1970-01-01
相关资源
最近更新 更多