简体   繁体   中英

Efficient thumbnail creation for PDF files like iBooks

How is iBooks able to create PDF page thumbnails so fast on first load? I tried using CGContext functions to draw the page and then resize it to get a thumbnail. But this approach takes way to long. Is there an efficient way to get thumbnails of PDF pages?

Thanks in advance, Anupam

First get all your PDF files Path in an array[here ie:pdfs].Then if you want to show all these PDF thumbnails in UICollectionView,just pass the index obtained from the collection view Delegate Method "CellForRowAtIndexPath" to the following,

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

Hope this would be fast enough to create thumbnail images for the PDF.

Disclaimer : I have not checked if this is faster or not.


There are some built in methods in ImageIO that are specialized in creating thumbnails. These methods should be optimized for creating thumbnails. You would need to add ImageIO.framework to your project and #import <ImageIO/ImageIO.h> in your code.

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM