首页 > 解决方案 > UIWebView 在加载 PDF 文件时没有释放内存

问题描述

我正在 UIWebView 中加载 pdf 文件(大小:15 MB)。

// If it is found, show it
[self.webView loadData:pdfData
              MIMEType:@"application/pdf"
      textEncodingName:@"UTF-8"
               baseURL:nil];

问题是当我多次加载文件时,RAM 使用率会更高,并且应用程序在某个点崩溃。当我弹出视图控制器时,占用的内存没有释放。

标签: iosobjective-cmemory-managementuiwebview

解决方案


只需在您的代码中添加三行,然后检查

-(void) dealloc
{
    [self.webview cleanForDealloc];
    self.webview = nil;
    [super dealloc];

}

更新:

当我删除视图控制器时调用下面的方法。

-(void)webViewUnload {

    [self.webView loadHTMLString:@"" baseURL:nil];
    [self.webView stopLoading];
    [self.webView setDelegate:nil];
    [self.webView removeFromSuperview];

    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}

推荐阅读