我在我的应用程序中使用了 UIWebView
。它通常运行良好,但有一种情况是应用程序收到内存警告并最终崩溃。
我用这个加载内容:
[self.webView loadHTMLString:htmlString baseURL:baseURL];
有一种情况,htmlString
包含超过 25 个 YouTube 视频(这不是我的想法——这是我收到的网页)。所以在这种情况下,应用会收到一些内存警告并最终崩溃。
我该如何处理这种情况?是否可以分步加载 HTML 文件?
我不知道这是否与它有任何关系,但我正在动态设置 UIWebView
大小——以及包含 Web View 的 ScrollView 的内容大小。这是代码:
- (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);
}
非常感谢,
卡洛斯
最佳答案
来个调皮的小把戏吧。使用以下代码获取页面的html:
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* newStr = [[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding];
使用正则表达式查找视频元素并替换为您的自定义图像超链接标签>。 将 html 字符串显示为:
[webView loadHTMLString: baseURL:nil];
关于ios - 在 UIWebView 中加载包含许多 YouTube 视频的 HTML 文件时,应用程序会收到内存警告并崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14324303/