簡體   English   中英

為iBooks等PDF文件高效創建縮略圖

[英]Efficient thumbnail creation for PDF files like iBooks

iBooks在首次加載時如何快速創建PDF頁面縮略圖? 我嘗試使用CGContext函數繪制頁面,然后調整其大小以獲取縮略圖。 但是這種方法要花很長時間。 有沒有一種有效的方法來獲取PDF頁面的縮略圖?

在此先感謝,Anupam

首先,將所有PDF文件的Path放入數組中(此處為:pdfs)。然后,如果要在UICollectionView中顯示所有這些PDF縮略圖,只需將從集合視圖Delegate方法“ CellForRowAtIndexPath”獲得的索引傳遞給以下對象,

-(UIImage *)GeneratingIcon:(int)index
{
    NSURL* pdfFileUrl = [NSURL fileURLWithPath:[pdfs objectAtIndex:index]];
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfFileUrl);
    CGPDFPageRef page;

    CGRect aRect = CGRectMake(0, 0, 102, 141); // thumbnail size
    UIGraphicsBeginImageContext(aRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage* IconImage;
    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 first PDF page
    page = CGPDFDocumentGetPage(pdf, 1);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, page);

    // Create the new UIImage from the context
    IconImage = UIGraphicsGetImageFromCurrentImageContext();

    CGContextRestoreGState(context);

    UIGraphicsEndImageContext();
    CGPDFDocumentRelease(pdf);

    return IconImage;
}

希望這將足夠快地為PDF創建縮略圖。

免責聲明 :我沒有檢查這是否更快。


ImageIO中有一些內置方法專門用於創建縮略圖。 這些方法應被優化以創建縮略圖。 您需要將ImageIO.framework添加到您的項目中,並在代碼中#import <ImageIO/ImageIO.h>

// Get PDF-data
NSData *pdfData = [NSData dataWithContentsOfURL:myFileURL];

// Get reference to the source
// NOTE: You are responsible for releasing the created image source
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)pdfData, NULL);

// Configure how to create the thumbnail
// NOTE: You should change the thumbnail size depending on how large thumbnails you need.
// 512 pixels is probably way too big. Smaller sizes will be faster.
NSDictionary* thumbnailOptions = 
    @{(id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
      (id)kCGImageSourceCreateThumbnailFromImageIfAbsent: (id)kCFBooleanTrue,
      (id)kCGImageSourceThumbnailMaxPixelSize: @512}; // no more than 512 px wide or high

// Create thumbnail
// NOTE: You are responsible for releasing the created image
CGImageRef imageRef = 
    CGImageSourceCreateThumbnailAtIndex(imageSourceRef,
                                        0, // index 0 of the source
                                        (__bridge CFDictionaryRef)thumbnailOptions);

// Do something with the thumbnail ...

// Release the imageRef and imageSourceRef
CGImageRelease (imageRef);
CFRelease(imageSourceRef);

暫無
暫無

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

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