无法创建超过60页的PDF和PRINT(内存增加和崩溃) [英] Can't Create PDF and PRINT more than 60 Pages ( memory Raises & crashes)

查看:111
本文介绍了无法创建超过60页的PDF和PRINT(内存增加和崩溃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



减少内存->在分派队列中运行也无济于事



找不到解决方案。请帮我解决这个问题...



,并在以下链接中提及,但无济于事



解决方案

您必须构建自己的编写类似这样的代码:

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

其中:
文件名可以像这样:

  NSString * newPDFName = [NSString stringWithFormat:@%@。pdf,@ whatEverNameYouWant]; 

NSArray *路径= NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);

NSString * documentsDirectory = [paths objectAtIndex:0];


NSString * pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];

NSLog(@%@,pdfPath);

基本上,此方法的主要好处是减少NSData,这会造成内存压力。
在所有代码上看起来都是这样:

  //设置我们要创建的pdf生成的是
UIGraphicsBeginPDFContextToFile(pdfPath,CGRectZero,nil);
int i = 0;
for(; i< pages; i ++)
{
@autoreleasepool {
//指定pdf页面的大小
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0 ,kDefaultPageWidth,kDefaultPageHeight),nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//将上下文移动到边距
CGContextTranslateCTM(currentContext,kMargin,kMargin);
//将图层绘制到pdf,忽略 renderInContext not found警告。
[tableView.layer.layer renderInContext:currentContext];
}
}
//所有操作都是通过制作pdf
UIGraphicsEndPDFContext();完成的。

就是这样!您可以计算一下


I need to generate PDF more than 60 pages and need to Print it, but in iPhone & iPad memory Ram raises to 350.50MB-500.00MB and Crashes .

For Reducing memory->Running in dispatch queues also that doesn't help

Can't find the solution for this . Plz help me in this ...

and referred below link but doesn't help

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

Generating Pages Code Starts

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

Memory Raises in iPad4 whereas in iPad Mini 180-240MB nd crashes

解决方案

you have to construct your code some thing like this:

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

here: file namecan be like this:

NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", @"whatEverNameYouWant"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];

    NSLog(@"%@",pdfPath);

basically the main benefit of this approach will be reduce NSData which is creating memory pressure. over all code will look some thing this:

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

Thats it !! you can take care of your calculation

这篇关于无法创建超过60页的PDF和PRINT(内存增加和崩溃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆