无法创建PDF和打印超过60页(内存提升和崩溃)

时间:2016-05-26 12:43:36

标签: ios objective-c iphone pdf cgcontext

我需要生成超过60页的PDF并且需要打印它,但是在iPhone& iPad内存Ram升至350.50MB-500.00MB和Crashes。

用于减少内存 - >在调度队列中运行也没有帮助

无法找到解决方案。 Plz帮助我...

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

Cannot create PDF document with 400+ pages on iOS

-(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内存崩溃 enter image description here

2 个答案:

答案 0 :(得分:1)

你必须构建一些像这样的代码:

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);

这种方法的主要好处基本上是减少NSData,这会产生内存压力。  所有代码都会看到一些事情:

// 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();

多数民众赞成!!你可以照顾你的计算

答案 1 :(得分:0)

这不是上述问题的答案,它是另一个尝试使用

生成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);

档案名称&amp;路径

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;

  }