Q:

Objective-C: How to add thumbanils to a TableView From Videos?

Bellow i have some code that reads and lists the names of files in the documents directory of my application. This works fine and i know want to add thumnails from each of the videos i get so i found this code:
NSURL *videoURL = [NSURL fileURLWithPath:@""];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
cell.imageView.image = thumbnail;
[player stop];

However I'm not sure where to point the path to my files so that it adds a different thumbnail for every separate video my tableview is displaying.
Here is my full code where I'm getting the video files and displaying in table: 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

    NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

    cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
    filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]];

    NSURL *videoURL = [NSURL fileURLWithPath:@""];
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

    UIImage *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    cell.imageView.image = thumbnail;
    [player stop];
    return cell;

}

Thanks in advance.

A:

You shouldn't be doing this in cellForRowAtIndexPath. Instead, you should do the thumbnail generation when the controller is loaded and save the images into an NSArray / NSCache / disk with appropriate names. Then when cellForRowAtIndexPath gets called you just get the appropriate image.
The code you have is close, you just want to loop over the file names in filePathsArray and actually create the videoURL from them.
Depending on how many there are you might also want to run this process on a background thread and just tell the table view to refresh as each one is loaded.

