使用 NSThread iPhone 下载 100 张图像时崩溃
我目前正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ASIHTTPRequest API 进行下载。
Use ASIHTTPRequest API for downloading.
您应该使用 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: