2013-01-15 47 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) 
} 
相關問題