UIWebViews的内存问题

时间:2012-02-15 08:28:12

标签: iphone objective-c ios uiwebview

我有一个webviewcontroller,我创建了一个实例,然后我更改了请求,并根据在tableview中选择了哪一行来显示webview。

// Replace tableView:didSelectRowAtIndexPath with the following
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{

CGRect rect = self.view.frame;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
rect.size.width = width; //self.view.frame.size.width;
rect.size.height = height; //self.view.frame.size.height;
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

page = [[WebViewController alloc] initWithUrl:entry.articleUrl];
page.view.frame = rect;
[self.view addSubview:page.view];

}

我的问题是,在加载其中一些后,我收到内存警告并崩溃。

在另一个标签页面中,我还有一个webview,可在加载标签时加载谷歌地图。这个应用程序几乎在加载后立即崩溃。

由于我只制作了每个实例,我不明白为什么会出现这个问题。以前我每次创建一个新的webviewcontroller,然后发布它,但我遇到了同样的问题。

以下是加载Google地图的代码:

@interface MapViewController : UIViewController {
    IBOutlet UIWebView *mapPage;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
url_string = @"http://maps.google.fr/maps/ms?msa=0&msid=213119412551786463007.0004b3fb7f4c123377402&z=12";
url = [NSURL URLWithString:url_string];
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_string]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
mapPage.scalesPageToFit = YES;
mapPage.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[mapPage loadRequest:request];
[super viewDidLoad];
}

UIWebView是在界面构建器中创建的。

在地图加载完毕后,或者在您尝试与地图交互时,内存耗尽并崩溃。它似乎与上面的问题完全相同,但这次没有选项卡,或者没有更改UIWebView使用的请求。

2 个答案:

答案 0 :(得分:2)

每次在UITableView中选择行时,都会创建一个WebViewController的新对象,并将其视图(出于某种原因?)添加到当前控制器的视图中。我不确定你至少从superview中删除该视图(你还必须发布WebViewController)。当然,它将需要越来越多的记忆。

创建整个UIViewController并将其用作子视图的原因是什么?为什么你没有使用UINavigationController?

答案 1 :(得分:2)

事实证明页面正在被缓存。

在我的应用程序的applicationDidReceiveMemoryWarning方法中,我添加了一行来清除缓存。这解决了我的p

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
 Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
 */
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
相关问题