We are trying to create multiple PDF files using the render method UIView.layer
The following code runs in autoreleasepool.
---loop UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectZero, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext:pdfContext]; view =nil; pdfContext =nil UIGraphicsEndPDFContext() --loop ends
After repeating several repeating resident memory, it increases to 20 mb, and the subsequent iteration adds to the total used memory.
Some, like ARC, do not free the memory used in pdf rendering, and therefore the application with low memory crashes.
Application uses ARC
Any help or pointers to solve the problem would be much appreciated.
thanks
UPDATED: Thanks for the quick reply.
I apologize for the typo in the pseudocode. Here is a sample code from a test project.
-(BOOL)addUIViewToPDFFile:(UIView*)view newBounds:(CGRect)newBounds pdfFile:(NSString*)pdfFile{ BOOL retFlag =YES; NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString* documentDirectory = [documentDirectories objectAtIndex:0]; NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:pdfFile]; UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectZero, nil); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); if([NSThread isMainThread]){ NSLog(@"Main Thread"); }else{ NSLog(@"Not in Main Thread"); } UIGraphicsBeginPDFPage(); logMemUsage1(); [view.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); pdfContext = nil; logMemUsage1(); return retFlag; } - (IBAction)createPdf:(id)sender { for(int i=0;i<10;i++){ @autoreleasepool { PDFViewController *vc = [[PDFViewController alloc] init]; [vc loadView]; NSString *PdfFileName =[ NSString stringWithFormat:@"TestPdf%d.pdf",i ]; [self addUIViewToPDFFile:vc.view newBounds:self.view.frame pdfFile:PdfFileName]; vc = nil; }
}}
Another point that needs to be added is that when the application goes into the background when the home device button is pressed and returns to the foreground, the memory seems to be cleared and freed.
I expect that after each iteration, the memory that is used to create the pdf should be released.