Crash while downloading 100 images using NSThread iPhone
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], indirilenDe开发者_Python百科rgi, 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..
Use ASIHTTPRequest API for downloading.
You should use NSURLConnection
by saving chunks of data to the disk each time you receive it through delegate's method: connection:didReceiveData:
精彩评论