使用 NSData 分配 Iphone

发布于 2024-09-26 21:31:48 字数 2013 浏览 0 评论 0 原文

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

-(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 方法完成也是如此。在仪器中,下载的每个图像都保持分配状态。随着每个图像的下载,总分配量持续增加,最终出现内存不足崩溃。然而,Instruments 不会报告与该对象相关的泄漏。

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

我尝试将自动释放池仅放在 updateImages 方法中,并且仅放在 downloadChartForDictionary 方法中,但都没有帮助。

我一整天都被困在这个问题上,所以非常感谢您的帮助

I have a method that downloads many images from the internet, then saves them to the device. Here is the code:

-(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;

}

My problem is that when this runs, the NSData object, data, is allocated but never released, even when the updateImages method is finished. In instruments, each image that is downloaded remains allocated. The total allocations continues to rise as each image is downloaded and eventually there is a out of memory crash. Instruments does not report a leak associated with this object however.

I have tried: [[NSData alloc]initWithContentsOfURL:url] and then releasing it, but that doesn't help.

I have tried putting the autorelease pool only in the updateImages method and only in the downloadChartForDictionary method and neither helps.

I've been stuck on this all day so any help is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-10-03 21:31:48

您是否尝试过[[[NSData alloc]initWithContentsOfURL:url] autorelease]?这会将 NSData 对象放入当前的自动释放池中。简单的分配/初始化创建仍然需要您的显式管理。

Did you try [[[NSData alloc]initWithContentsOfURL:url] autorelease]? That will put the NSData object into the current autorelease pool. A straightforward alloc/init creation will still require your explicit management.

洋洋洒洒 2024-10-03 21:31:48

尝试使用 dataWithContentsOfURL:options:error: 具有不同的阅读选项。我将从 NSDataReadingUncached,因为听起来你的数据在你不希望它被缓存的时候被缓存了。

Try using dataWithContentsOfURL:options:error: with different reading options. I would start with NSDataReadingUncached, because it sounds like your data is being cached when you don't want it to be.

并安 2024-10-03 21:31:48

这实际上不是问题......出于某种原因,仪器正在报告分配,但当我实际运行该应用程序时,它从未崩溃。

this was actually a non-issue... For some reason instruments was reporting the allocations, but when I actually ran the app, it never crashed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文