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

标签 ios memory-management uiwebview

我在我的应用程序中使用了 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/

相关文章:

c++ - C++中的内存分配分析

c++ - 如何正确使用 std::shared_from_this 或绕过它?

objective-c - 关于dispatch_async和nsdata的xcode ios ARC(自动引用计数)

html - UIWebView 和 100% 高度网页

ios - 如何在 iOS WebView 中启用 peek 和 pop?

javascript - 无法使用 JavaScript 在 iphone 上播放声音但可以在 Android 上播放

iOS UITextView 生成所选文本的屏幕截图 - 是否可以使用 TextKit 或任何其他方法?

ios - 为什么 AVPlayer 在 'playbackBufferEmpty' 之后立即以 'playbackLikelyToKeepUp' 状态失败?

ios - 在嵌入式 YT IFrame 上禁用 iOS 弹性滚动

ios - UIWebView 开始加载但从未完成