使用 NSThread iPhone 下载 100 张图像时崩溃

发布于 2024-11-28 11:05:15 字数 1496 浏览 1 评论 0原文

我目前正在开发一个 iPhone 杂志应用程序,该应用程序可以从互联网下载杂志内容。杂志页面为图像格式,约有 120 页。

这是我解决问题的方法。

- (void) downloadDergiPages {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *resourcePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [resourcePaths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Dergilerim"];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:indirilenSayi];


for (int i=1; i<=numberOfPages; i++) {

    NSString *url = [NSString stringWithFormat:@"%@getdergipage?dergikey=%@&page=%d", [RepositoryInfo getHostName], indirilenDergi, i];
    NSLog(@"%@", url);

    NSData *receivedData =  [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",i]];
    [receivedData writeToFile:path atomically:YES];
    [receivedData release];

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:i] waitUntilDone:YES];
}


[self performSelectorOnMainThread:@selector(removePopupView) withObject:nil waitUntilDone:YES];
//[self removePopupView];

[pool release];
} 

虽然此代码片段在 iPad 应用程序中运行没有问题,但它会导致应用程序在第 44 次完成后崩溃 iPhone 中的页面下载。据我从日志控制台了解,这是因为内存问题。 我只想获取图像并将其一张一张保存到磁盘。我正在释放所有分配“receivedData”。 这段代码可能有什么问题,我该如何让它工作?

先感谢您..

I am currently working on an iPhone magazine application which downloads its magazine content from internet. The magazine pages are in image format and it has about 120 pages.

This is my solution to problem.

- (void) downloadDergiPages {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *resourcePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [resourcePaths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Dergilerim"];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:indirilenSayi];


for (int i=1; i<=numberOfPages; i++) {

    NSString *url = [NSString stringWithFormat:@"%@getdergipage?dergikey=%@&page=%d", [RepositoryInfo getHostName], indirilenDergi, i];
    NSLog(@"%@", url);

    NSData *receivedData =  [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",i]];
    [receivedData writeToFile:path atomically:YES];
    [receivedData release];

    [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithInt:i] waitUntilDone:YES];
}


[self performSelectorOnMainThread:@selector(removePopupView) withObject:nil waitUntilDone:YES];
//[self removePopupView];

[pool release];
} 

Although this code fragment works in iPad application with no problem, it causes app to crash after the finish of the 44th
page download in iPhone. It is because of the memory issues as I understand from log console.
I just want to get images and save to disk one by one. I am releasing all the allocations "receivedData".
What may be the problem with this code and how can I make it work?

Thank you in advance..

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

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

发布评论

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

评论(2

瘫痪情歌 2024-12-05 11:05:15

使用 ASIHTTPRequest API 进行下载。

Use ASIHTTPRequest API for downloading.

浮生未歇 2024-12-05 11:05:15

您应该使用 NSURLConnection

每次通过委托方法接收数据时,将数据块保存到磁盘:connection:didReceiveData:

You should use NSURLConnection

by saving chunks of data to the disk each time you receive it through delegate's method: connection:didReceiveData:

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