简体   繁体   English

为iBooks等PDF文件高效创建缩略图

[英]Efficient thumbnail creation for PDF files like iBooks

How is iBooks able to create PDF page thumbnails so fast on first load? iBooks在首次加载时如何快速创建PDF页面缩略图? I tried using CGContext functions to draw the page and then resize it to get a thumbnail. 我尝试使用CGContext函数绘制页面,然后调整其大小以获取缩略图。 But this approach takes way to long. 但是这种方法要花很长时间。 Is there an efficient way to get thumbnails of PDF pages? 有没有一种有效的方法来获取PDF页面的缩略图?

Thanks in advance, Anupam 在此先感谢,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, 首先,将所有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;
}

Hope this would be fast enough to create thumbnail images for the PDF. 希望这将足够快地为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. ImageIO中有一些内置方法专门用于创建缩略图。 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. 您需要将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