0

I've searched a lot but fail to find any solution. I'm working on app that create video and saved at local directory and showing these saved video on front screen of the app. But I want to show only 6 latest saved videos on screen. How can I get the latest videos path from directory . Please help.

This is code which I've used to get all video files

[[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];
        }
   } }
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • This [SO question](http://stackoverflow.com/questions/2108953/ios-how-do-you-find-the-creation-date-of-a-file) may help you, it shows how to retrieve the creation date. – Lukasz 'Severiaan' Grela Apr 24 '14 at 07:29

2 Answers2

1

maybe you can try to search for the creation date and take the last 6 that have been created, doing like this:

[[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
            }
        }
    } }*

hope this can help you, otherwise we will find something else to do.

Norolim
  • 926
  • 2
  • 10
  • 25
0

Yes, I've got it. Save the video with current date and use this method. This will return sorting items array.

-(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;   }
  • The other answer is better, given *creation date* is part of a file's *metadata* (therefore you don't need to include it in the filename unless you are trying to avoid name clashes). – trojanfoe Apr 24 '14 at 09:34
  • @trojanfoe: yes I want like you but I'm quit new to objective C and the other answer is unclear for me would you like to show me how can I use? – Altaf Bangash Apr 24 '14 at 09:47