iphone - webView 中的简单视频和内存消耗

标签 iphone objective-c ios memory video

在 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];

enter image description here

最佳答案

从您的代码来看,在我看来,您正在尝试将 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/

相关文章:

ios - 为什么核心显卡线条不对齐?

ios - 电影未加载

ios - Swift:关闭在当前上下文中显示图片的 ViewController 时,屏幕有时会卡住

ios - 如何将 CBDescriptor 值转换为字符串?

ios - UILabel 未在 dispatch_group_notify 内部更新

ios - 使用 UIDocumentPickerViewController,是否可以像在 Slack 中一样在首次打开时显示默认服务(Dropbox、Google Drive 等)?

iphone - 在编辑模式下选择单元格时未调用 didSelectRowAtIndexPath

javascript - 尝试在 Mobile Safari 上调试 Javascript 但当 iPhone 插入 Mac 时 "Enable web inspector on device"在 Desktop Safari 中变灰

iphone - 如何将属性设为私有(private)?

iOS 应用程序文本看起来更大已通过使用启动屏幕修复