3

I have the following code to read PDF page.
The problem is at each time I read the a page the instruments analyzing give me about 1MB increase of CFData (store)? any help is appreciated.

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context;

context = CGBitmapContextCreate(NULL, 
                                    width, 
                                    height, 
                                    8,
                                    width* 4,
                                    colorSpace, 
                                    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width*2, pageSize.height*2));

CGContextSaveGState(context);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
CGRect rect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGRect rectClip = CGContextGetClipBoundingBox(context);
CGAffineTransform transform = aspectFit(rect,rectClip);
CGContextConcatCTM(context, transform);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);

UIImage *img = [UIImage imageWithCGImage:image];
CGImageRelease(image);
Omar Freewan
  • 2,678
  • 4
  • 25
  • 49

2 Answers2

2

I was having the same issue and found your question while searching for an answer.

In my case I was converting the pdf pages into jpegs and then saving them to disk. My trouble seems to have been in the ownership and release of the CGPDFDocumentRef and the CFURLRef.

Using a _bridge cast on NSURL *pdfURL fixed the issue for me.
I thought this answer did a good job of explaining bridged casts: ARC and bridged cast

The Analyze tool in XCode is very helpful in these types of situations.

- (void) drawRect:(CGRect)rect {
    NSURL *pdfURL = [NSURL fileURLWithPath:self.filePath];

    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL); 

    for (int i = 1; i <= (int)CGPDFDocumentGetNumberOfPages(document); ++i){
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
        CGContextClearRect(context, self.bounds);
        CGContextFillRect(context, self.bounds);

        CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, i);

        CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);

        CGFloat scale = MIN(self.bounds.size.width / pageRect.size.width, self.bounds.size.height / pageRect.size.height);

        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextScaleCTM(context, scale, scale);

        CGContextSetInterpolationQuality(context, kCGInterpolationLow);
        CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
        CGContextDrawPDFPage(context, pageRef);

        CGContextRestoreGState(context);

        CGImageRef image = CGBitmapContextCreateImage(context);

        NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@_%i.jpg", self.sourceId, i]];
        [UIImageJPEGRepresentation([UIImage imageWithCGImage:image], 1) writeToFile:jpgPath atomically:YES];

        CGImageRelease(image);
    }

    CGPDFDocumentRelease(document);
}

Hope this helps someone!
Kevin

Community
  • 1
  • 1
kevinstueber
  • 2,926
  • 3
  • 26
  • 26
2

After long tracing I found the problem was I did not close the pdf document after I finished using it

CGPDFDocumentRelease(pdfDoc);  

if not you keep all allocated pages inside the problem

Omar Freewan
  • 2,678
  • 4
  • 25
  • 49