2010-10-08 67 views
1

我有一种方法可以从互联网下载许多图像,然后将它们保存到设备。下面是代码:Iphone与NSData分配

-(IBAction) updateImages { 
    for (x=0; x<[imagesToUpdate count]; x=x+1) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    NSMutableDictionary *tempDic = [dic objectForKey:[imagesToUpdate objectAtIndex:x]]; 
    BOOL newDictionary = [self downloadChartForDictionary:tempDic]; 

    [pool drain]; 
    } 

} 


-(BOOL) downloadChartForDictionary:(NSMutableDictionary *)dictionary { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    NSMutableDictionary *updatedDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary]; 
    NSString *downloadString = [NSString stringWithFormat:@"http://www.photo.com/%@_0001.jpg",[updatedDictionary objectForKey:@"PDFName"]]; 
    [updatedDictionary setObject:serverAIRAC forKey:@"airac"]; 
    NSDate *date = [NSDate date]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    formatter.dateFormat = @"MM-dd-y"; 
    NSString *dateString = [formatter stringFromDate:date]; 
    [updatedDictionary setObject:dateString forKey:@"date"]; 
    [formatter release]; 


    NSURL *url = [NSURL URLWithString:downloadString]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 


    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
    NSUserDomainMask, YES); 
    NSString *docsPath = [paths objectAtIndex:0]; 


    NSString *savePath = [NSString stringWithFormat:@"%@/%@^%@_%@.jpg",docsPath,serverAIRAC,[updatedDictionary objectForKey:@"airport"],[updatedDictionary objectForKey:@"PDFName"]]; 

    BOOL downloaded = [data writeToFile:savePath atomically:YES]; 

    [pool drain]; 

    return YES; 

} 

我的问题是,当这个运行时,NSData对象,数据,被分配但永远不会释放,即使updateImages方法完成。在乐器中,每个下载的图像都保持分配。随着每个图像的下载,总分配量不断增加,并最终导致内存不足。仪器不报告与此对象相关的泄漏。

我试过了:[[NSData alloc] initWithContentsOfURL:url],然后释放它,但这没有帮助。

我已经尝试将autorelease池只在updateImages方法中,并且只在downloadChartForDictionary方法中,并且都没有帮助。

我一直停留在这个整天所以任何帮助是极大的赞赏

+0

有什么令人沮丧的是我有另一个控制器几乎完全相同的东西,它完美的作品。 – Brodie 2010-10-09 00:17:54

回答

0

你尝试[[[NSData alloc]initWithContentsOfURL:url] autorelease]?这将把NSData对象放入当前的自动释放池中。直接的alloc/init创建仍然需要你的明确管理。

+0

不,但我只是没有欢乐,谢谢你的建议。 – Brodie 2010-10-08 22:33:58

0

这实际上是一个非问题......由于某些原因,仪器报告了分配,但是当我真正运行应用程序时,它从未崩溃。