Can't Create PDF and PRINT more than 60 Pages ( memory Raises & crashes) [duplicate]

本秂侑毒 提交于 2019-12-11 12:46:02

问题


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


回答1:


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




回答2:


It is not the answer for above question , it is another Code which was tried for to generate PDf using

UIGraphicsBeginPDFPageWithInfo. This approach also crashes for more than 500 rows nothing but it comes around 56 Pages

After this approach after returning PDF Data when i assign that PDF Data to UIPrinterInteractionController Action -

It shows , SO i am unable to calculate Pages

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.

Note : whereas this Printer error doesn't shows in Above code which i posted above with 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);

File Name & 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);

Generating Pages

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

After GraphicsEnd retrieving NSData from FilePath

   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;

  }


来源:https://stackoverflow.com/questions/37461231/cant-create-pdf-and-print-more-than-60-pages-memory-raises-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!