1

下面我有一些代码可以读取并列出我的应用程序文档目录中的文件名。这很好用,我知道想从我得到的每个视频中添加缩略图,所以我找到了这段代码:

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

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

但是,我不确定将路径指向我的文件的位置,以便为我的 tableview 显示的每个单独视频添加不同的缩略图。

这是我获取视频文件并在表格中显示的完整代码:

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


}

提前致谢。

4

1 回答 1

0

你不应该在cellForRowAtIndexPath. 相反,您应该在加载控制器时生成缩略图,并将图像保存到具有适当名称的NSArray//NSCache磁盘中。然后,当cellForRowAtIndexPath被调用时,您只需获得适当的图像。

您拥有的代码很接近,您只想遍历文件名filePathsArray并实际创建videoURL它们。

根据有多少,您可能还想在后台线程上运行此过程,并告诉表视图在每个加载时刷新。

于 2013-08-25T15:55:53.157 回答