Generation of large PDF file thumbnails causing crash on iPhone/iPad
I have written code to generate thumbnails from pdf files & save them as png pictures, but the same code that runs successfully on iPad 2 would crash on iPad 1.
After loading up instruments I saw at one point the memory usage spikes up to 20mb when generating the pictures for half a dozen pdfs, then it would drop back to normal levels after the CG drawing is done, I'm pretty sure the intensive memory load is causing the crash because I replaced that portion of the code with loading normal pictures & it worked fine on both devices.
So the question is: how to generate pdf thumbnails with CG functions and use as little memory as possible? I'm thinking of running it on a background thread but I don't think that would optimize the memory usage in any way...
The code is also applying a gradient effect on the thumbnail as well, that's a functional requirement...
Here's the code:
+ (UIImage *)imageFromPDFWithDocumentRef:(NSString *)documentPath{
CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:documentPath]);
CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
if(pageRect.size.width >pageRect.size.height){
pageRect.origin.x = (pageRect.size.width - pageRect.size.height)/2;
pageRect.size.width = pageRect.size.height;
}else{
pageRect.origin.y = (pageRect.size.height - pageRect.size.width)/2;
pageRect.size.height = pageRect.size.width;
}
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));
CGContextDrawPDFPage(context, pageRef);
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 0.1, // Start color
0.0, 0.0, 1.0, 0.7 }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGRect currentBounds =开发者_如何学Go pageRect;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 800);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), 0);
CGContextDrawLinearGradient(context, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
CGRect thumbRect = CGRectMake(0, 0, 187, 187);
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGPDFDocumentRelease(documentRef);
return [UICommon resizedImage:finalImage rect:thumbRect];
}
Thanks!
Calling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
before CGContextDrawPDFPage
solved a similar problem of mine.
Credits goes to this answer of Johann: CGContextDrawPDFPage taking up large amounts of memory
精彩评论