Advertisement
markos-lacerda

Create PDF Thumb

Jan 12th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +(UIImage *)createPDFThumb:(NSString *)pdfURL thumbSize:(CGSize)thumbSize {
  2.     NSURL *url = [NSURL URLWithString:pdfURL];
  3.     CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef) url);
  4.     CGPDFPageRef page;
  5.    
  6.     CGRect aRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);
  7.     UIGraphicsBeginImageContext(aRect.size);
  8.     CGContextRef context = UIGraphicsGetCurrentContext();
  9.    
  10.     UIImage* thumbnailImage;
  11.    
  12.     NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
  13.    
  14.     for (int i = 0; i < totalNum; i++ ) {
  15.         CGContextSaveGState(context);
  16.         CGContextTranslateCTM(context, 0.0, aRect.size.height);
  17.         CGContextScaleCTM(context, 1.0, -1.0);
  18.        
  19.         CGContextSetGrayFillColor(context, 1.0, 1.0);
  20.         CGContextFillRect(context, aRect);
  21.        
  22.         // Grab the first PDF page
  23.         page = CGPDFDocumentGetPage(pdf, i + 1);
  24.         CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
  25.  
  26.         // And apply the transform.
  27.         CGContextConcatCTM(context, pdfTransform);
  28.         CGContextDrawPDFPage(context, page);
  29.        
  30.         // Create the new UIImage from the context
  31.         thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
  32.        
  33.         CGContextRestoreGState(context);
  34.     }
  35.    
  36.     UIGraphicsEndImageContext();    
  37.     CGPDFDocumentRelease(pdf);
  38.    
  39.     return thumbnailImage;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement