views:

64

answers:

1

I have following code

NSString *filePath=[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:pdfname ofType:@"pdf" inDirectory:@"appMasterPdf"]]; 
NSURL *url = [NSURL fileURLWithPath:filePath];
[filePath release];

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);
CGFloat scaleRatio; 
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, pageNumber);
pageRect = CGPDFPageGetBoxRect(page, kCGPDFBleedBox);
width=pageRect.size.width;
height=pageRect.size.height;
if(pageRect.size.width/pageRect.size.height < 1.0) {
    scaleRatio = height/pageRect.size.height;
}
else {
    scaleRatio = width/pageRect.size.width;
}

CGFloat xOffset = 0.0;
CGFloat yOffset = height;
if(pageRect.size.width*scaleRatio<width) {
    xOffset = (width/2)-(pageRect.size.width*scaleRatio/2);
}
else {
    yOffset = height-((height/2)-(pageRect.size.height*scaleRatio/2));
}

CGContextTranslateCTM(context, xOffset, yOffset);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page,
                                                              kCGPDFBleedBox, CGRectMake(0, 0, pageRect.size.width, pageRect.size.height),
                                                              0, true);
pdfTransform = CGAffineTransformScale(pdfTransform, scaleRatio, scaleRatio);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndPDFContext();
UIGraphicsEndImageContext();

CGContextRelease(context);
CGPDFPageRelease(page);
return tempImage;

This function is acalling in for loop

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url); These lines give me a memory leaks. Please help me . Thanx in advanced

+1  A: 

From the manual of CGPDFDocumentCreateWithURL, "You are responsible for releasing the object using CGPDFDocumentRelease."

So you need to call

CGPDFDocumentRelease(pdf)
when you are done. The general convention of CG methods is that if the name contains Create then there will be a corresponding Release method which you must call.

taskinoor
It generates an error bad accees
Where are you adding this?
taskinoor
i added after thisUIGraphicsEndPDFContext();UIGraphicsEndImageContext(); 2 lines
sorry to say that im not getting any clue :-(
taskinoor
pdfTransform = CGAffineTransformScale(pdfTransform, scaleRatio, scaleRatio);CGContextConcatCTM(context, pdfTransform);CGContextDrawPDFPage(context, page);UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();CGContextRestoreGState(context);UIGraphicsEndPDFContext();UIGraphicsEndImageContext();CGPDFDocumentRelease(pdf)I put like this