在 WebView 中播放视频并查看乐器时 - 我看到播放时内存使用量达到高峰。 (总共约 23 MB)
当我离开 View (它在 UINavigation View 中)时,所有内存都会按预期清除。 (使用 ARC)
重要提示:我正在从磁盘加载视频而不是从服务器加载视频!
问题:有没有办法减少播放视频时的内存?
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: videoURL cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 10.0];
[webView loadRequest: request];
[webView setOpaque:NO];
最佳答案
从您的代码来看,在我看来,您正在尝试将 UIWebView 用作视频播放器,而不同时在其中显示任何其他 HTML 内容。
尽管这是很有可能的,但正如您所观察到的那样,它并不是特别有效 - UIWebView 会将其所有内容加载到内存中,因为它是为显示网页而设计的。
更好的解决方案是使用 Apple 的 MediaPlayer
框架,即MPMoviePlayerController
和/或 MPMoviePlayerViewController
.
如果你只需要播放全屏视频,你应该使用MPMoviePlayerViewController
.使用它很简单:
MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:vc];
[vc release];
这将呈现一个包含您的剪辑的模态视图 Controller 。
如果你想自定义它的任何部分,你可以使用 moviePlayer
属性。
如果您更愿意在另一个 View 中显示视频,您应该查看MPMoviePlayerController
.
使用此类涉及更多样板,但也为您提供了更多控制权:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player prepareToPlay];
player.view.frame = contentView.bounds; //The Player View's Frame must match the Parent View's
// ...
[player play];
关于iphone - webView 中的简单视频和内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8835298/