开发者

Memory warning while creating thumbnails of a PDF in Objective C

To create the thumbnails of a PDF, I tried these sections of code in my project. Depending on the contents of the PDFs of course, I get memory warnings which in my opinion should be fixable.

I found out that if I leave out the CGContextConcatCTM method calls, then they seem OK. But I need what this method does for a proper thumbnail. Here are the methods I tried:

-(UIImage *)getThumbForPage:(int)page_number{
 CGFloat width = 60.0;

    // Get the page
 CGPDFPageRef page = [self getPage:page_number];

 CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
 CGFloat pdfScale = width/pageRect.size.width;
 pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
 pageRect.origin = CGPointZero;


 UIGraphicsBeginImageContext(pageRect.size);

 CGContextRef context = UIGraphicsGetCurrentContext();

 // White BG
 CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
 CGContextFillRect(context,pageRect);

 CGContextSaveGState(context);

    // ***********
 // Next 3 lines makes the rotations so that the page look in the right direction
    // ***********
 CGContextTranslateCTM(context, 0.0, pageRect.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);
 CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true));

 CGContextDrawPDFPage(context, page);
 CGContextRestoreGState(context);

 UIImage *thm = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();
 return thm;

}

And the other is:

NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
CGPDFPageRef page;

CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage* thumbnailImage;


NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);

for(int i = 0; i < totalNum; i++ ) {


    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, aRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetGrayFillColor(context, 1.0, 1.0);
    CGContextFillRect(context, aRect);


    // Grab the first PDF page
    page = CGPDFDocumentGetPage(pdf, i + 1);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, page);

    // Create the new UIImage from the context
    thumbnailImage = UIGraphicsGetImageFro开发者_如何学编程mCurrentImageContext();

    //Use thumbnailImage (e.g. drawing, saving it to a file, etc)

    CGContextRestoreGState(context);

}


UIGraphicsEndImageContext();    
CGPDFDocumentRelease(pdf);

Any suggestions on how I can overcome this?


Calling

CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

before CGContextDrawPDFPage solved my problem.

Credits goes to this answer of Johann: CGContextDrawPDFPage taking up large amounts of memory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜