How to delete the cache from UIWebview or dealloc UIWebview(如何从 UIWebview 或 dealloc UIWebview 中删除缓存)
问题描述
每次我使用 UIWebView
加载新页面时,之前加载的页面都会显示一小段时间.
Every time I load a new page with UIWebView
the before loaded page is shown for a short time.
如何清除该缓存?另一种可能性是释放 UIWebview
.我试过了,但我的 UIWebView
总是空的".这种情况下alloc
和dealloc
应该怎么做呢?
How can I clear that cache? Another possibility would be to dealloc UIWebview
. I tried that but than my UIWebView
is always "empty". How should the alloc
and dealloc
be done in this case?
我注意到 UIWebView
消耗了大约 10 MB RAM.现在 UIWebView
与 ViewController
一起加载.并且视图是自动发布的,并且 UIWebView
是自动发布的.每次释放 WebView 不是更好吗?
I noticed that the UIWebView
is consuming about 10 MB RAM. Now the UIWebView
is loaded together with the ViewController
. And the view is autoreleased as well as the UIWebView
is autoreleased. Wouldn't it be better to dealloc the WebView each time?
解决方案:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 0, 320, 480);
self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];
self.webView.scalesPageToFit = YES;
[self.view addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.webView removeFromSuperview];
self.webView = nil;
}
推荐答案
我对这个问题的解决方案是在 viewWillAppear:
上以编程方式创建 UIWebView
并在viewDidDisappear:
,像这样:
My solution to this problem was to create the UIWebView
programmatically on viewWillAppear:
and to release it on viewDidDisappear:
, like this:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.webView = [[[UIWebView alloc]initWithFrame:exampleFrame] autorelease];
self.webView.scalesPageToFit = YES;
self.webView.delegate = self;
self.webView.autoresizingMask = webViewBed.autoresizingMask;
[exampleView addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;
}
如果你这样做并且你的 UIWebView
没有被释放,你应该检查它的保留计数并了解谁在保留它.
If you do this and your UIWebView
doesn't get released you should check it's retain count and understand who is retaining it.
这篇关于如何从 UIWebview 或 dealloc UIWebview 中删除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 UIWebview 或 dealloc UIWebview 中删除缓存
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01