UIWebView内存泄漏解决办法(一)

来源:互联网 发布:凯撒和众信 知乎 编辑:程序博客网 时间:2024/05/17 21:48

UIWebView


UIWebView的内存问题,其实在iOS7以前就一直存在,但是由于webView加载的内容,程序员是无法控制的,所以一直没有一个很好的解决办法。最近,公司的项目也要做有关与h5的交互。所以,会大量使用UIWebView,为了防止内存泄漏,而造成程序闪退问题,所以,还是需要解决这个问题:研究了一段,流行的一个解决办法就是:网络缓存和释放UIWebView中的多余资源;

具体如下“

1、关闭缓存

<pre name="code" class="objc">-(void)webViewDidFinishLoad:(UIWebView *)webView{    //关闭缓存    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];    [[NSUserDefaults standardUserDefaults] synchronize];}
2、退出该页面的时候,需要释放不需要的资源:
-(void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];    [_web loadRequest:nil];    [_web removeFromSuperview];    _web = nil;    _web.delegate = nil;    [_web stopLoading];}

3、当收到内存警告的时候,需要处理网络的缓存:
-(void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    [[NSURLCache sharedURLCache] removeAllCachedResponses];}

这是现在流行的一个解决办法,但是,效果甚微。
还需要继续研究。




0 0