下载大量图像时发生内存泄漏问题
我试图在屏幕顶部向用户下载多于600张图像,并在屏幕上方显示进度表。我使用淡入淡出层来屏蔽我的屏幕,以显示活动和进度。下载大量图像时发生内存泄漏问题
我收到之间的内存警告消息和应用程序崩溃。
我的步骤,以达到循环是:
- 在应用程序委托,我检查第一核心数据表针对其具有isImageAvailable布尔字段“0”值的所有行。
- 如果显示我一些计数(比如说600),并且我用YES和NO选项显示并提醒。
- 在是:[self performSelector:@selector(myDownload :) withObject:nil afterDelay:0.2];
-
在myDownload
NSOperationQueue *queue = [NSOperationQueue new]; // Create our NSInvocationOperation to call loadDataWithOperation, passing in nil NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(startUpdatingRecords:) object:nil]; // Add the operation to the queue [queue addOperation:operation]; [operation release]; [queue release];
-
在startUpdatingRecords:
-(void)startUpdatingRecords:(id)sender { [self performSelectorInBackground:@selector(updateProgressMeter:) withObject: [NSString stringWithFormat:@"%d",self.loopStartIndex]]; // Variable declarations CGSize newSizeLarge ; NSPredicate *predicate; NSMutableArray *MatchingID; Image_DB *data; // Cache Directory path NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSData *responseData; // = [[NSData alloc]init] ; NSURL *url = [[[NSURL alloc]init] autorelease]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init] autorelease]; UIImage *imgSelected_Large = [[[UIImage alloc]init] autorelease]; // Loop through all IDs for (int i = 0; i < [self.arrayOfID count]; i++) //for (int i = loopStart; i < loopEnd; i++) { if (self.abortDownload) { break; } NSString *documentsDirectory = [[[NSString alloc] initWithFormat:@"%@",[paths objectAtIndex:0]] autorelease]; documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingFormat:@"/ImageFolder"]; // Image folder path myClass *classObj = [self.arrayOfID objectAtIndex:i]; NSString *strURl = [[[NSString alloc] initWithFormat:@"%@%@", self.MyURL,recipeObj.recipeImageStr] autorelease]; //NSLog(@"URL = %@",strURl); url = [NSURL URLWithString:strURl]; request = [NSMutableURLRequest requestWithURL:url]; responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; // Get Image Data into NSData //imgSelected_Large = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strURl]]]; NSLog(@"Download Count = %d",i+1); if (responseData != nil) { imgSelected_Large = [UIImage imageWithData:responseData]; // Resizining image newSizeLarge.width = 320; newSizeLarge.height = 180; imgSelected_Large = [self imageWithImage:imgSelected_Large scaledToSize:newSizeLarge]; // New sized image NSData *dataPhoto; // no need to release it because UIImageJPEGRepresentation gives autoreleased NSData obj. dataPhoto = UIImageJPEGRepresentation(imgSelected_Large, 0.6); // Set new image representation and its Compression Quality documentsDirectory = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"Image_%d", classObj.nodeID]]; [dataPhoto writeToFile:documentsDirectory atomically:YES]; //Write file to local folder at default path predicate = [NSPredicate predicateWithFormat: @"(image_ID = %d)",recipeObj.nodeID]; MatchingID = [CoreDataAPIMethods searchObjectsInContext:@"Image_DB" :predicate :@"image_ID" :YES :self.managedObjectContext]; // Setting flag variable for available image for (int j = 0; j< [MatchingID count]; j++) { //Assign the Authors Records in Class Object and save to Database data = (Image_DB*) [MatchingID objectAtIndex:j]; // data.image_large = dataPhoto; // Code for storing BLOB object to DB data.extra_1 = @"1"; //NSLog(@"Flag updated"); } } // Exit out code if (i == [self.arrayOfID count] - 1 || i == [self.arrayOfID count]) // Its the last record to be stored { NSError *error; if (![self.managedObjectContext save:&error]) { // Handle the error... NSLog(@"Error in updating %@",error); } self.isUpdateImageCalled = NO; [self performSelectorOnMainThread:@selector(removeProgressMeter) withObject:nil waitUntilDone:NO]; } // Update UI screen while in downloading process [self performSelectorInBackground:@selector(updateProgressMeter:) withObject:[NSString stringWithFormat:@"%d",self.loopStartIndex+i+1]]; } }
如果我没有释放responseData然后我的应用程序显示我记忆警告,并得到了坠毁。如果我发布了,那么[NSConcreteMutableData release]:发送消息发送到解除分配的实例0x1e931de0错误发生。
如何优化我的代码。任何人都可以在我的代码中建议我并重写并制作精致的代码。
请帮我一把。
您的responseData
返回sendSynchronousRequest
是autoreleased因此你不应该自己释放它。对于第一眼,我没有看到代码中的内存泄漏。有可能您的应用程序实际上使用太多内存,而不会泄漏它。尝试把一个自动释放池的内部进行循环:
for (...) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// your original code with a lot of autoreleased objects
[pool release];
}
如果一个自动释放池内包装你的代码,所有发送包裹内autorelease
消息的所有对象将被真正发布的时候池本身是发布:这种方式可以在每个循环中清除内存。
另请参阅文档中的Using Autorelease Pools,它特别提到您应该在“如果您编写创建许多临时对象的循环”的情况下使用它们。
非常感谢,它为我工作。再次感谢。 :) – 2012-04-06 08:33:42
愿这个链接可以帮助你! http://stackoverflow.com/questions/3703734/downloading-large-amount-of-images-ipad-memory-leak – NSException 2012-04-06 07:42:32