This app is for ipad magazine. We need to read the PDF and convert to a PNG image for thumnail. After reading the PDF before the conversion that calls CGContextDrawPDFPage, the memory rises quickly and is never freed until it crashes. I tried in many ways, but it did not work. I really appreciate if anyone can help.
CGContextDrawPDFPage (context, aPage); // this is a memory killer
Below is the full code:
- (UIImage *) image: (CGPDFPageRef) aPage rect: (CGRect) aRect {CGRect pdfcropBox = CGRectIntegral (CGPDFPageGetBoxRect (aPage, kCGPDFCropBox));
if ((float)pdfcropBox.size.width/(float)pdfcropBox.size.height >
(float)aRect.size.width/(float)aRect.size.height)
{
aRect.size.height = aRect.size.width * pdfcropBox.size.height / pdfcropBox.size.width;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,aRect.size.width,
aRect.size.height,
8,
(int)aRect.size.width * 4,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
colorSpace = nil;
CGPDFPageRetain(aPage);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(aPage,
kCGPDFCropBox,
CGRectMake(0, 0, aRect.size.width,aRect.size.height),
0, true);
CGContextSaveGState(context);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, aPage);
CGPDFPageRelease (aPage);
aPage = nil;
CGContextRestoreGState(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage *resultingImage = [UIImage imageWithCGImage:image];
CGContextClearRect(context, aRect);
CGContextClearRect(context, pdfcropBox);
CGContextRelease(context);
CGImageRelease(image);
context = nil;
NSLog(@"colorSpace :%d,aPage :%d,context :%d",[colorSpace retainCount],[aPage retainCount],[context retainCount]);
return resultingImage;
}
source
share