加载/卸载 HTML5
我开发了一个带有多个 UIWebView 的 iPad 应用程序,它采用以下 HTML 和 JavaScript。由于 iPad 一次只能播放一个视频,因此在特定的 Web 视图聚焦之前我不会加载视频。
这是通过调用 UIWebView 上的 stringByEvaluatingJavaScriptFromString 方法,发送对 JS 方法 getFocus() 的调用来完成的。同样,当不再需要 Web 视图时,我调用lostFocus() 来卸载视频。这使得另一个 UIWebView 可以播放另一个视频。
到目前为止,一切都很好。一切都很完美。除了一件事:这会导致内存泄漏。每当观看时,
我都尝试重新加载 UIWebView(而不是使用 JS 卸载视频),但这不起作用。我还尝试了无数不同的 JavaScript 变体、标签变体等等。
<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'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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于解决了这个问题!解决方案是清空 src 并“加载”不存在的视频。这不会导致内存泄漏。看看:
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: