簡體   English   中英

在iOS中創建PDF到圖像

[英]PDF to image creation in ios

大家好我正試圖從我的應用程序的pdf創建PNG(縮略圖)圖像為我使用下面的代碼堆棧溢出的幾個答案是我正在使用` -

- (無效)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張縮略圖。

* 問題:*

1.代碼有問題嗎?

2.我在上面的代碼塊中犯了什么錯誤?

3.是否有其他機制可以從PDF創建png圖像?

在此先感謝您的建議對我來說更重要。

一千個頁面的循環意味着您為每個頁面進程分配內存,並且在方法結束之前永遠不會重用該內存塊。 Obj-C內存管理以這種方式工作,每次循環后都有效釋放內存。

如果要為每次迭代重用內存,請使用獨立的NSAutoreleasePool

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

或者,如果您使用ARC:

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

查看這個開源項目; 他們在將PDF轉換為UIImage方面做得相當不錯。 https://github.com/mindbrix/UIImage-PDF

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM