memory warning, generating UIImagefrom pdf files in objective-c

Posted by favo on Stack Overflow See other posts from Stack Overflow or by favo
Published on 2010-05-18T23:17:12Z Indexed on 2010/05/18 23:20 UTC
Read the original article Hit count: 733

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about xcode

Related posts about iphone