-(void)thumbnailOfPDF{
NSURL* pdfFileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"test" ofType:@"pdf"]];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)CFBridgingRetain(pdfFileUrl));
CGPDFPageRef page;
CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage* thumbnailImage;
NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
[self.thumbScrollView setContentSize:CGSizeMake(self.thumbScrollView.frame.size.width, totalNum*100)];
for(int i = 0; i < totalNum; i++ ) {
UIImageView *showThumbnail = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100*i, 80, 80)];
[showThumbnail setBackgroundColor:[UIColor redColor]];
//Add the thumb Image on the ScrollView
[self.thumbScrollView addSubview:showThumbnail];
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, i + 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
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
[showThumbnail setImage:thumbnailImage];
//Use thumbnailImage (e.g. drawing, saving it to a file, etc)
CGContextRestoreGState(context);
}
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);
}
Comments
Post a Comment