在UIWebView中加载包含许多YouTube视频的HTML文件时,应用会收到内存警告和崩溃

Car*_*los 8 memory-management uiwebview ios

UIWebView在我的应用程序中使用了一个.它通常运行良好,但有一种情况,应用程序收到内存警告,并最终崩溃.

我用这个加载内容:

[self.webView loadHTMLString:htmlString baseURL:baseURL];
Run Code Online (Sandbox Code Playgroud)

有一个案例中htmlString有超过25个YouTube视频(这不是我的想法 - 这是我收到的网页).因此,在这种情况下,应用程序会收到一些内存警告,最后崩溃.

我该如何处理这种情况?是否可以在各个步骤中加载HTML文件?

我不知道这是否与它有任何关系,但我正在设置UIWebView大小 - 以及包含Web视图的滚动视图的内容大小 - 动态.这是代码:

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    // Adaptamos las vistas al contenido y ocultamos el indicador de actividad

    // Ponemos el webView del tamaño justo del contenido

    CGRect frame = webView.frame;
    // Hace falta cambiar la height porque si no, no coge los cambios. Visualmente no se ve diferencia
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size.height = fittingSize.height;
    webView.frame = frame;

    // Movemos el botón y lo ponemos donde acabe el webView
    CGRect buttonFrame = self.visitSiteButton.frame;
    buttonFrame.origin.y = frame.origin.y + frame.size.height + 20;
    self.visitSiteButton.frame = buttonFrame;

    // Ampliamos el contenSize del scrollview general para que quepa todo el webView
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.visitSiteButton.frame.origin.y + self.visitSiteButton.frame.size.height + 10);
}
Run Code Online (Sandbox Code Playgroud)

非常感谢,

卡洛斯

ser*_*gio 0

单个 HTML 页面中的 25 个 YouTube 视频似乎太多了:内存已满,应用程序被终止。

我怀疑如果您不愿意修改 HTML 页面的设计,那么还有很多事情要做。但如果你愿意的话,还有很多选择。例如,您可以实现 HTML 的延迟加载(通过 javascript),以便只显示少数视频(如 UITableView 中)。或者,您可以将与 youtube 链接的 HTML 代码替换为其他一些“较轻”的代码,然后让用户加载新的 UIWebView 来观看特定视频。ETC。