问题描述
每次使用 UIWebView
加载新网页时,系统会在短时间内显示之前加载的网页。
如何清除缓存?另一种可能性是dealloc UIWebview
。我试过,但比我的 UIWebView
始终是空。在这种情况下应该如何处理 alloc
和 dealloc
?
我注意到 UIWebView
正在消耗大约10 MB RAM。现在 UIWebView
与 ViewController
一起加载。并且视图是自动释放的,以及 UIWebView
是自动释放的。
pre>
- (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:
,像这样:
- (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
没有得到释放,你应该检查它的保留计数,并明白谁保留它。
Every time I load a new page with UIWebView
the before loaded page is shown for a short time.
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?
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?
Solution:
- (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;
}
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;
}
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中删除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!