memory warning, generating UIImagefrom pdf files in objective-c
- by favo
When converting PDF Pages into UIImages I receive memory warnings all the time.
It seems there is either some leak or something else that eats my memory.
Using instruments didn't give me any helpful details.
I'm using the following function to generate images from a pdf file:
- (UIImage*)pdfImage:(NSString*)pdfFilename page:(int)page {
CFURLRef pdfURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)pdfFilename, kCFURLPOSIXPathStyle, false);
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfRef, page);
CGRect pdfPageSize = CGPDFPageGetBoxRect(pdfPage, kCGPDFBleedBox);
float pdfScale;
if ( pdfPageSize.size.width < pdfPageSize.size.height ) {
pdfScale = PDF_MIN_SIZE / pdfPageSize.size.width;
}
else {
pdfScale = PDF_MIN_SIZE / pdfPageSize.size.height;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
pdfPageSize.size.width*pdfScale,
pdfPageSize.size.height*pdfScale,
8,
(int)pdfPageSize.size.width*pdfScale * 4,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
// CGContextClipToRect(context, pdfPageView.frame);
// CGPDFPageRetain(pdfPage);
CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(pdfPage, kCGPDFBleedBox),
CGContextGetClipBoundingBox(context));
CGContextConcatCTM(context, transform);
CGContextDrawPDFPage(context, pdfPage);
// CGPDFPageRelease (pdfPage);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *finalImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);
CGPDFDocumentRelease(pdfRef);
return finalImage;
}
I am releasing the document and everything else, so where could be the problem?
Thanks for your help!