PDF到ios中的图像创建 [英] PDF to image creation in ios

查看:113
本文介绍了PDF到ios中的图像创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正试图从我的应用程序的pdf创建PNG(缩略图)图像,因为我使用了以下代码中的stackoverflow的几个答案,我正在使用` -

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 {

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

}`

* 问题: *上面的代码在模拟器中运行良好,并且在设备中工作以创建一些图像(最多30个缩略图)。我使用分析器检查内存泄漏但是上面的代码没有泄漏但我仍然无法创建所有缩略图。我使用的是包含1037页的ebook.pdf(300MB)。所以我需要1037个缩略图。

*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.

* 问题:*

1.代码有问题吗?

1.Is any problem with the code?

2.我在上面的代码块中做的错误是什么?

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

3.是否存在从PDF创建png图像的任何其他机制?

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

在此先感谢您的建议对我来说更重要。

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

推荐答案

千页的循环意味着您为每个页面进程分配内存,并且在方法结束之前永远不会重用该内存块。 Obj-C内存管理以这种方式工作,在每次循环后有效释放内存。

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.

如果要为每次迭代重用内存,请使用独立的NSAutoreleasePool

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];
}

或者,如果您使用ARC:

Or, in the case you are using ARC:

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

这篇关于PDF到ios中的图像创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆