PDF to image creation in ios

岁酱吖の 提交于 2019-12-18 18:23:11

问题


Hi Everyone I am trying to create PNG(thumbnail) images from the pdf for my app for that i used couple of answers from the stackoverflow below code is i am using `-

-(void)pdfToImageConverter{

NSURL* pdfFileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ebook" ofType:@"pdf"]];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
CGPDFPageRef page;

CGRect aRect = CGRectMake(0, 0, 100, 200); // thumbnail size


NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);

for(int i = 0; i < totalNum; i++ ) {
    CGPDFPageRef myPageRef=CGPDFDocumentGetPage(pdf, i+1);
     aRect=CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    UIGraphicsBeginImageContext(CGSizeMake(383, 383*(aRect.size.height/aRect.size.width)));
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage* thumbnailImage;
    //thumbnailImage=[[UIImage alloc] init];
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, aRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetGrayFillColor(context, 1.0, 1.0);
    CGContextFillRect(context, aRect);

    // Grab the  PDF page
    page = CGPDFDocumentGetPage(pdf, i + 1);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
    CGContextDrawPDFPage(context, page);

    // Create the new UIImage from the context
    thumbnailImage = [UIGraphicsGetImageFromCurrentImageContext() retain];

    //Use thumbnailImage (e.g. drawing, saving it to a file, etc)

    CGContextRestoreGState(context);
    NSString *documentsPath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingFormat:@"/page_%d.png",i+1];
    NSData *imagedata;//=[[NSData alloc] init];
    imagedata=[UIImagePNGRepresentation(thumbnailImage) retain];
    [thumbnailImage release];
    [imagedata writeToFile:documentsPath atomically:YES];
    [imagedata release];
    UIGraphicsEndImageContext();   


}
CGPDFDocumentRelease(pdf);
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"message" message:@"images creation completed" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
[alert release];

}`

*Problem:*Above code is working well in simulator and its working in device for creating some images(up to 30 thumbnails ).I used analyzer to check memory leaks but there is no leaks in above code but still i am unable to create all the thumbnails.I am using ebook.pdf(300MB) which contains 1037 pages.so i need 1037 thumbnails.

*Question : *

1.Is any problem with the code?

2.What is the mistake i am doing in the above block of code?

3.Is there any other mechanism to create png images from the PDF?

Thanks in advance,your suggestions are more important for me.


回答1:


A loop for thousand pages means you alloc memory for each page process and that chunk of memory is never reused until method ends. Obj-C memory management works that way, with an effective release of memory after each loop.

If you want to reuse memory for each iteration, use an independent NSAutoreleasePool

for(int i = 0; i < totalNum; i++ ) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    ... .... (process page ... ) ...
    [pool release];
}

Or, in the case you are using ARC:

for(int i = 0; i < totalNum; i++) {
    @autoreleasePool {
        .... (... process page ... ) ....
    }
}



回答2:


Check out this open source project; they're doing quite a good job of rending the PDF into a UIImage. https://github.com/mindbrix/UIImage-PDF



来源:https://stackoverflow.com/questions/8652472/pdf-to-image-creation-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!