Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从UIWebView引用存储在NSDocumentDirectory中的视频_Javascript_Ios_Objective C_Html_Video - Fatal编程技术网

Javascript 从UIWebView引用存储在NSDocumentDirectory中的视频

Javascript 从UIWebView引用存储在NSDocumentDirectory中的视频,javascript,ios,objective-c,html,video,Javascript,Ios,Objective C,Html,Video,我正在构建一个混合web应用程序和本机iOS应用程序,并希望显示已在本地设备上录制并存储在NSDocumentDirectory中的视频,以便在UIWebView中显示 目前,基于以下代码,这似乎不起作用-该文件存在,但我认为从HTML引用该文件可能不正确 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDi

我正在构建一个混合web应用程序和本机iOS应用程序,并希望显示已在本地设备上录制并存储在NSDocumentDirectory中的视频,以便在UIWebView中显示

目前,基于以下代码,这似乎不起作用-该文件存在,但我认为从HTML引用该文件可能不正确

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];
NSString  *videoPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",fileName]];

BOOL fileExist =  [[NSFileManager defaultManager] fileExistsAtPath:videoPath];    
if(fileExist) {
    NSLog(@"File Exist in path");
} else {
    NSLog(@"NO File Exist");
}

videoPath = [videoPath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
videoPath = [videoPath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *htmlString = [NSString stringWithFormat:@"<video width=\"320\" height=\"240\" controls><source src=\"%@\" type=\"video/mp4\"></video>", [NSString stringWithFormat:@"file://%@/",videoPath]];
NSString *js = [NSString stringWithFormat:@"document.getElementById('videoList').innerHTML = '%@'", htmlString];
NSString *result = [self.livingWebView stringByEvaluatingJavaScriptFromString:js];

NSLog(@"Result: %@", result);

The output of the JavaScript injection is:

Result: <video width="320" height="240" controls><source src="file:////var//mobile//Applications//76FE06FB-39D3-4658-8B52-AFE9DBE0C524//Documents//23-07-2013-16-54-48.mp4/" type="video/mp4"></video>
NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*videoPath=[DocumentsDirectoryStringByAppendingPathComponent:[NSString stringWithFormat:@“%@.mp4”,文件名]];
BOOL fileExist=[[NSFileManager defaultManager]fileExistsAtPath:videoPath];
如果(文件存在){
NSLog(@“路径中存在文件”);
}否则{
NSLog(@“不存在文件”);
}
videoPath=[videoPath stringByReplacingOccurrencesOfString:@/“with string:@/”];
videoPath=[videoPath STRINGBYREPLACINGOCURNCESOFSTRING:@“带字符串:@”%20“];
NSString*htmlString=[NSString stringWithFormat:@],[NSString stringWithFormat:@“文件://%@/”,视频路径]];
NSString*js=[NSString stringWithFormat:@“document.getElementById('videoList')。innerHTML='%@',htmlString];
NSString*result=[self.livingWebView stringByEvaluatingJavaScriptFromString:js];
NSLog(@“结果:%@”,结果);
JavaScript注入的输出是:
结果:

感谢您的帮助。

您需要使用
NSURL*url=[NSURL fileURLWithPath:videoPath]
将路径转换为文件://URL。然后把它放到你的htmlString里。不要使用StringByReplacingOccurrencesofString执行手动操作,否则会丢失一些转换