开发者

How do I handle memory warnings when generating UIImages from PDF files in Objective-C?

开发者 https://www.devze.com 2022-12-30 14:23 出处:网络
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.

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!


Well, everytime you call this method, assuming you keep the result around, the autoreleased object allocated in

UIImage *finalImage = [UIImage imageWithCGImage:image];

will consume some memory. The iPhone is pretty unforgiving, so if you have a few of these images (or even just one), and some reasonably large pdf's, you could hit the memory limit pretty easily.

I've just been working with a 3000 x 2000 pixel image, and hit memory warnings all the time, until I started tiling it.

So,

1) how large are your pdf's pages?

2) are you keeping them around?

3) If you comment out the line mentioned above, and just return nil instead, do you still hit the memory warnings?


Leaks does sometimes give false positives or may even be reporting leaks that are not in your code. What does the analyzer (shift-option-A) say? I would trust it first. Another thing to check is whether your leak problem goes away with a move to a new version of the simulator.

0

精彩评论

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

关注公众号