【发布时间】:2014-12-04 17:41:33
【问题描述】:
我正在 UIWebview 中加载 URL,它工作正常。但是在加载 Web 请求期间,它消耗了太多内存。每当我在 UIWebview 中加载一些 URL 时,内存从 30mb 增加到 95mb,并且基于在 UIWebview 中单击的每个链接它仍在增加并达到 180mb 等等。我在 UIWebview 中使用了一些代码来删除内存。但没有任何好处。我已经通过 Xcode 中的分析完成了内存管理,但没有泄漏,我还检查了分配和泄漏。它只是创建了一个 UIWebview 实例。
这是我的代码:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
ViewController.h
@property(nonatomic,strong)UIWebview *webview;
ViewController.m @synthesize 网络视图;
-(void)viewdidLoad
{
NSString *urlString = @"some url";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview=[uiwebview alloc]init];
[self.webview loadRequest:request];
}
/*webview 委托
-(void)viewWillDisappear:(Bool)animated
{
self.webview=nil;
[self.webview removefromSuperView]
[self.webview loadHtmlString:@"" baseUrl:nil]
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
我这几天都面临这个问题。请给我一些替代方法或方法来删除 UIWebviewmemory。
【问题讨论】:
标签: ios objective-c url ios7 uiwebview