2013-01-15 32 views
0

我在显示一个UITableView的tmp目录的内容,但不能得到每个单元的文件路径。如何在UITableView中获取视频的文件路径? (tmp目录)

当按下电池,我的应用程序应该发挥与MPMoviePlayer的文件,但并非如此。

这是我到目前为止有:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 


NSString *tmpDirectory = NSTemporaryDirectory(); 
fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray 

NSString *movieURL = [fileList objectAtIndex:indexPath.row]; 

NSURL *url = [NSURL URLWithString:movieURL]; 

_moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 

[self presentModalViewController:_moviePlayerViewController animated:YES]; 
} 

的NSLog给我视频的名称,而不是路径。我觉得我正在从睡眠不足的角度思考这个问题。

回答

2

当您使用此:

NSString *movieURL = [fileList objectAtIndex:indexPath.row]; 

它会在临时目录中只返回文件名, 所以用这个:

NSString *movieURL = [tmpDirectory stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]]; 

NSURL*url = [NSURL fileURLWithPath:movieURL]; 
+0

哇。非常感谢,我不能相信我忘记了stringByAppendingPathComponent。 – CokePokes

+0

@CokePokes:高兴,感谢您的评论:) –

1

更改您的代码,如:

NSString *tmpDirectory = NSTemporaryDirectory(); 
    NSLog(@"%@",tmpDirectory); 
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:nil]; //nsarray 

    NSString *movieURL = [fileList objectAtIndex:indexPath.row]; 
    NSString *path=[tmpDirectory stringByAppendingPathComponent:movieURL]; 
+1

@CokePokes:其实Midhun熔点在我面前给出正确的答案。答案是在我写答案时加载的,这就是为什么发生重复。 –

1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let tmpDirectory: String = NSTemporaryDirectory() 
    print("\(tmpDirectory)") 
    var fileList: [Any]? = try? FileManager.default.contentsOfDirectory(atPath: tmpDirectory) 
    //nsarray 
    let movieURL: String? = (fileList?[indexPath.row] as? String) 
    let path: String = URL(fileURLWithPath: tmpDirectory).appendingPathComponent(movieURL!).absoluteString 
    print(path) 
} 
相关问题