2011-02-16 95 views
2

iBooks如何在第一次加载时如此快速地创建PDF页面缩略图?我尝试使用CGContext函数来绘制页面,然后调整大小以获得缩略图。但是这种方法需要很长时间。有没有一种有效的方式来获得PDF页面的缩略图?为iBooks等PDF文件创建高效缩略图

由于提前, 阿努邦

+0

我也面临同样的问题,但至今没有运气。 – ajay 2011-12-29 06:49:36

回答

0

首先获得您的所有PDF文件的路径在数组[这里即:PDF文件]。然后,如果你想显示在UICollectionView所有这些PDF缩略图,只是通过从所获得的指数集合视图的委托方法“的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创建缩略图。

0

免责声明:我还没有检查,如果这是更快或没有。


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