How to solve the problem of increasing CFData (store) memory?

I have the following code to read a PDF page.
The problem is that every time I read the page the analysis of the tools gives me roughly 1MB of increase in 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);

      

+3


source to share


2 answers


After a lot of tracing, I found the problem was that I didn't close the PDF document after I used it

CGPDFDocumentRelease(pdfDoc);  

      



If you haven't saved all the highlighted pages inside the problem

+2


source


I had the same problem and found your question while looking for an answer.

In my case, I converted the pdf pages to jpegs and then saved them to disk. My problem seemed to be in the ownership and release of CGPDFDocumentRef and CFURLRef.

Using _bridge cast did not NSURL *pdfURL

fix the problem for me.
I thought this answer explained bridging broadcasts pretty well: ARC and bridged listing



The parser in Xcode is very useful in situations like this.

- (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

+2


source







All Articles