加载/卸载HTML5时iOS Safari内存泄漏< video> [英] iOS Safari memory leak when loading/unloading HTML5 <video>
问题描述
我开发了一个带有几个UIWebViews的iPad应用程序,它采用以下HTML和JavaScript。由于iPad一次只能播放一个视频,因此我不会加载视频,直到关注特定的网络视图。
这是通过调用 UIWebView上的stringByEvaluatingJavaScriptFromString 方法,向JS方法 getFocus()发送调用。同样,当Web视图不再需要时,我会调用lostFocus()来卸载视频。这使得另一个UIWebView可以播放另一个视频。
到目前为止,这么好。一切都很完美。除了一件事:这会导致内存泄漏。每次查看时,我都试着重新加载UIWebView(而不是使用JS卸载视频),那没用。
< div id =videoDiv>>我也尝试了数十种不同的JavaScript变体,标签变体等等。 ;< / DIV>
< script type =text / javascript>
var movieDiv = document.getElementById('videoDiv'),
movieHtml ='< video id =videosrc =../ Documents /<%= VideoFileName%> width =768height =911>< / video>';
var gotFocus = function(){
movieDiv.innerHTML = movieHtml;
var movie = document.getElementById('video');
movie.play();
movie.addEventListener('pause',function(){
movie.currentTime = 0;
movie.play();
},false);
};
var lostFocus = function(){
movieDiv.innerHTML ='';
};
< / script>
我终于解决了这个问题!解决方案是清空src并加载一个不存在的视频。这不会导致内存泄漏。看看:
< div id =videoDiv>< / div>
< script type =text / javascript>
var movieDiv = document.getElementById('videoDiv'),
movieHtml ='< video id =videosrc =width =768height =911><视频>';
movieDiv.innerHTML = movieHtml;
var movie = document.getElementById('video');
var gotFocus = function(){
movie.src ='../Documents/<%= VideoFileName%>';
movie.load();
movie.play();
movie.addEventListener('pause',function(){
movie.currentTime = 0;
movie.play();
},false);
};
var lostFocus = function(){
movie.src ='';
movie.load(); //此行可能被删除,请参阅评论
};
< / script>
I've developed an iPad app with several UIWebViews that takes the following HTML and JavaScript. Since the iPad can only play one video at a time, I don't load the video until the particular web view is focused.
This is done by calling the stringByEvaluatingJavaScriptFromString method on the UIWebView, sending in a call to the JS method getFocus(). Similarly, when the web view is no longer needed I call lostFocus() to unload the video. This enables another UIWebView to play another video.
So far, so good. Everything works perfectly. Except for one thing: This causes a memory leak. Whenever viewing
I've tried reloading the UIWebView (instead of unloading the video with JS), that didn't work. I've also tried a zillion of different JavaScript variations, tag variations et cetera.
<div id="videoDiv"></div>
<script type="text/javascript">
var movieDiv = document.getElementById('videoDiv'),
movieHtml = '<video id="video" src="../Documents/<%= VideoFileName %>" width="768" height="911"></video>';
var gotFocus = function () {
movieDiv.innerHTML = movieHtml;
var movie = document.getElementById('video');
movie.play();
movie.addEventListener('pause', function () {
movie.currentTime = 0;
movie.play();
}, false);
};
var lostFocus = function () {
movieDiv.innerHTML = '';
};
</script>
I finally solved this! The solution was to empty the src and "load" a non-existing video. This causes no memory leak. Have a look:
<div id="videoDiv"></div>
<script type="text/javascript">
var movieDiv = document.getElementById('videoDiv'),
movieHtml = '<video id="video" src="" width="768" height="911"></video>';
movieDiv.innerHTML = movieHtml;
var movie = document.getElementById('video');
var gotFocus = function () {
movie.src = '../Documents/<%= VideoFileName %>';
movie.load();
movie.play();
movie.addEventListener('pause', function () {
movie.currentTime = 0;
movie.play();
}, false);
};
var lostFocus = function () {
movie.src = '';
movie.load(); // This line may be removed, see comments
};
</script>
这篇关于加载/卸载HTML5时iOS Safari内存泄漏< video>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!