无法创buildPDF和PRINT超过60页(内存引发和崩溃)

我需要生成PDF超过60页,需要打印它,但在iPhone和iPad内存拉姆升至350.50MB – 500.00MB和崩溃。

对于减less内存 – >在调度队列中运行也没有帮助

找不到解决scheme。 Plz帮助我…

并提到下面的链接,但没有帮助

无法在iOS上创build超过400页的PDF文档

-(NSData*)getPdfFullLineSheetiPhone:(UIScrollView *)tableView GridCount:(NSInteger)count{ // -- first page height, rest pages height: adjust to get it right #define FIRST_PAGE_HEIGHT_FULLSON 1040 #define REST_PAGES_HEIGHT_FULLSON 1090//1420 #define WIDTH_FULLSO_PORTRAITN 400 CGSize fittedSize; CGRect priorBounds = tableView.frame; // - the '200' is the cell height for estimating how many pages, and 200/3 is ROw calculation(How many rows in GMGridView) fittedSize =CGSizeMake(WIDTH_FULLSO_PORTRAITN, count * 200/3); tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height); 

生成页面代码开始

 CGRect pdfPageBounds; // Standard US Letter dimensions 8.5" x 11" pdfPageBounds = CGRectMake(0, 0, 768/1.8, REST_PAGES_HEIGHT_FULLSON/1.79); NSMutableData *pdfData = [[NSMutableData alloc] init]; UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); int pageno=0; { // do page1 CGRect pdfPageBoundsPage1; pdfPageBoundsPage1 = CGRectMake(0,0,768/1.8, FIRST_PAGE_HEIGHT_FULLSON/1.7); UIGraphicsBeginPDFPageWithInfo(pdfPageBoundsPage1, nil); { CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 10, 0); [tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; pageno ++; } //Rest of Pages for (CGFloat pageOriginY = FIRST_PAGE_HEIGHT_FULLSON/1.7; pageOriginY < fittedSize.height; pageOriginY += REST_PAGES_HEIGHT_FULLSON/1.79) { @autoreleasepool { UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); { CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 10, -pageOriginY); [tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; pageno ++; } } } } UIGraphicsEndPDFContext(); tableView.bounds = priorBounds; return pdfData; } 

内存在iPad4上升,而在iPad Mini上则上升180-240MB nd崩溃 在这里输入图像说明

你必须像这样构build你的代码:

 UIGraphicsBeginPDFContextToFile( pdfPath, CGRectZero, nil );// as per rMaddy UIGraphicsBeginPDFPageWithInfo(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [tableView.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); 

这里:文件名可以是这样的:

 NSString *newPDFName = [NSString stringWithFormat:@”%@.pdf”, @"whatEverNameYouWant"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; NSLog(@”%@”,pdfPath); 

基本上这种方法的主要好处是减lessNSData,这是造成内存压力。 在所有的代码将看起来这样的事情:

 // Set up we the pdf we're going to be generating is UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); int i = 0; for ( ; i < pages; i++) { @autoreleasepool{ // Specify the size of the pdf page UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); CGContextRef currentContext = UIGraphicsGetCurrentContext(); // Move the context for the margins CGContextTranslateCTM(currentContext, kMargin, kMargin); // draw the layer to the pdf, ignore the "renderInContext not found" warning. [tableView.layer.layer renderInContext:currentContext]; } } // all done with making the pdf UIGraphicsEndPDFContext(); 

而已 !! 你可以照顾你的计算

这不是上述问题的答案,它是另一个被试用于生成PDf的代码

UIGraphicsBeginPDFPageWithInfo。 这种方法也崩溃超过500行,但它约56页

在将PDF数据分配给UIPrinterInteractionController时返回PDF数据之后的这种方法操作 –

它显示,所以我无法计算页面

 Print-Job failed: Printer exists. 2016-05-27 00:37:26.131 APPName[9078:2952235] \032Send\032to\032Mac\032@\032macminiB._ipp._tcp.local.: startJob not called. 

注意:虽然这个打印机错误不显示在上面的代码,我用UIGraphicsBeginPDFContextToData张贴

 -(NSData *)getPdfSimpleSOTr:(UITableView *)tableView{ #define FIRST_PAGE_HEIGHT 1188 #define REST_PAGES_HEIGHT 1176.5 CGSize fittedSize; CGRect priorBounds; // 140208 dan - Comment: save the WIDTH CGRect savedFrame = tableView.frame; // 140207 dan - force portrait width priorBounds = tableView.frame; priorBounds.size.width=768; // put into Portrait tableView.frame = priorBounds; fittedSize = [tableView sizeThatFits:CGSizeMake(priorBounds.size.width, ([tableView numberOfRowsInSection:0] * 49) + 529)]; tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height); CGRect pdfPageBounds; pdfPageBounds = CGRectMake(0, -12, 768, REST_PAGES_HEIGHT); 

文件名称和path

 NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", @"AppName"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; NSLog(@"%@",pdfPath); 

生成页面

 // Set up we the pdf we're going to be generating is UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); int pageno=0; { CGRect pdfPageBoundsPage1 = CGRectMake(0,0,768, FIRST_PAGE_HEIGHT+15);//15 UIGraphicsBeginPDFPageWithInfo(pdfPageBoundsPage1, nil); { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 10, 0); [tableView.layer renderInContext:context]; pageno ++; } for (CGFloat pageOriginY = FIRST_PAGE_HEIGHT; pageOriginY < fittedSize.height; pageOriginY += REST_PAGES_HEIGHT) { @autoreleasepool{ // Specify the size of the pdf page UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); CGContextRef context = UIGraphicsGetCurrentContext(); // Move the context for the margins CGContextTranslateCTM(context, 10, -pageOriginY); // draw the layer to the pdf, ignore the "renderInContext not found" warning. [tableView.layer renderInContext:context]; } } } // all done with making the pdf UIGraphicsEndPDFContext(); 

GraphicsEnd从FilePath中检索NSData之后

  NSData *pdfData; if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) { pdfData = [[NSFileManager defaultManager] contentsAtPath:pdfPath]; } else { NSLog(@"File not exits"); } tableView.bounds = priorBounds; // 140208 dan - Comment: restored the saved WIDTH tableView.frame=savedFrame ; return pdfData; }