Iphone 使用NSdata下载PDF后清除缓存和内存
使用此代码时,我将如何清理缓存数据和其他内存? 模拟机上的CFData(存储)不断增长Iphone 使用NSdata下载PDF后清除缓存和内存,iphone,objective-c,caching,nsdata,Iphone,Objective C,Caching,Nsdata,使用此代码时,我将如何清理缓存数据和其他内存? 模拟机上的CFData(存储)不断增长 -(void)downloadFile:(NSURL *)theURL { NSLog(@"dowbload this url : %@",theURL); NSURL *url = theURL; NSData *data = [NSData dataWithContentsOfURL:url]; NSArray *paths = NSSearchPathForDire
-(void)downloadFile:(NSURL *)theURL
{
NSLog(@"dowbload this url : %@",theURL);
NSURL *url = theURL;
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"PDF2.pdf"];
[data writeToFile:pdfPath atomically:YES];
[self showThePDF];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache removeAllCachedResponses];
[sharedCache release];
}
您需要释放变量:data、path和pdfPath。以下电话:
[NSData dataWithContentsOfURL:url]
NSSearchPathForDirectoriesInDomains
[documentsDirectory stringByAppendingPathComponent:@"DPR2.pdf"]
返回您为避免内存泄漏而需要释放的所有自有对象。您自己没有对它们调用init,但它们的内部实现代表您分配了内存,然后保留了该内存(这就是为什么您可以使用这些变量而不用担心出现分段错误)。即使您的变量在方法返回时超出范围,它们分配的内存仍将保留,因为您没有通过释放减少其引用计数。在此之前,我展示了大致修改过的代码:
-(void)downloadFile:(NSURL *)theURL
{
NSLog(@"dowbload this url : %@",theURL);
NSURL *url = theURL;
NSData *data = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"DPR2.pdf"];
[data writeToFile:pdfPath atomically:YES];
[self showThePDF];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache removeAllCachedResponses];
[sharedCache release];
[pdfPath release];
[paths release];
[data release];
}
您可以选择在一个数据库中管理动态分配的变量。我最近遇到了类似的问题 本质上,dataWithContentsOfURL在引擎盖下使用NSURLConnection,它缓存响应 我想推荐几件事: 您可以自己使用NSURLConnection来获取数据,而不是带有URL内容的数据 使用异步NSURLConnection API和委托方法(很少需要同步方法) 实现下面的NSURLConnection委托方法,并在其中返回nil:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return nil;
}
这确保了响应不会被缓存
NSURLConnection文档:
使用NSURLConnection:
使用此代码
我对包含URL内容的数据也有同样的问题。尝试添加@autorealse:
@autoreleasepool {
NSError *error = nil;
NSData *data=[[NSData alloc] init];
data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
img = [UIImage imageWithData:data];
}
哇,好吧,有人否决了我的答案。你想对你不同意的内容发表评论吗?不是我投的反对票,但是我不明白为什么我应该发布这些项目,因为我既不初始化也不复制它们。此外,你发布的上述代码也会因为同样的原因崩溃,也许你想详细说明为什么发布我没有分配的东西。亲爱的Perception,我不同意,代码崩溃只是为了证明我是对的,你自己试试,你会看到的。谢谢你的时间,不过据我所知你是不正确的,一个工作代码会证明我是错的,但我很理解,然后解决问题本身。我会在访问Mac时尝试,如果我确实是错的,我会编辑我的答案。同时,您可能需要分析代码,以确保您正在释放所有拥有的对象。所有这些变量都是自动释放的,不应该手动释放-ShiShi在这里是正确的。