Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

iphone - Image of the first pdf page ios sdk

Do you know how to save into a UIImageView the first page of a pdf file? I have to create the preview for the pdf. Do you have any idea?

Thanks for help Nicco

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following method will build a thumbnail from a PDF file. It is RetinaDisplay-aware, so the thumbnails should be especially crisp on such devices.

- (UIImage *)buildThumbnailImage
{
  BOOL hasRetinaDisplay = FALSE;  // by default
  CGFloat pixelsPerPoint = 1.0;  // by default (pixelsPerPoint is just the "scale" property of the screen)

  if ([UIScreen instancesRespondToSelector:@selector(scale)])  // the "scale" property is only present in iOS 4.0 and later
  {
    // we are running iOS 4.0 or later, so we may be on a Retina display;  we need to check further...
    if ((pixelsPerPoint = [[UIScreen mainScreen] scale]) == 1.0)
      hasRetinaDisplay = FALSE;
    else
      hasRetinaDisplay = TRUE;
  }
  else
  {
    // we are NOT running iOS 4.0 or later, so we can be sure that we are NOT on a Retina display
    pixelsPerPoint = 1.0;
    hasRetinaDisplay = FALSE;
  }

  size_t imageWidth = 320;  // width of thumbnail in points
  size_t imageHeight = 460;  // height of thumbnail in points

  if (hasRetinaDisplay)
  {
    imageWidth *= pixelsPerPoint;
    imageHeight *= pixelsPerPoint;
  }

  size_t bytesPerPixel = 4;  // RGBA
  size_t bitsPerComponent = 8;
  size_t bytesPerRow = bytesPerPixel * imageWidth;

  void *bitmapData = malloc(imageWidth * imageHeight * bytesPerPixel);

  // in the event that we were unable to mallocate the heap memory for the bitmap,
  // we just abort and preemptively return nil:
  if (bitmapData == NULL)
    return nil;

  // remember to zero the buffer before handing it off to the bitmap context:
  bzero(bitmapData, imageWidth * imageHeight * bytesPerPixel);

  CGContextRef theContext = CGBitmapContextCreate(bitmapData, imageWidth, imageHeight, bitsPerComponent, bytesPerRow,
                                                  CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);

  CGPDFDocumentRef pdfDocument = MyGetPDFDocumentRef();  // NOTE: you will need to modify this line to supply the CGPDFDocumentRef for your file here...
  CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDocument, 1);  // get the first page for your thumbnail

  CGAffineTransform shrinkingTransform =
    CGPDFPageGetDrawingTransform(pdfPage, kCGPDFMediaBox, CGRectMake(0, 0, imageWidth, imageHeight), 0, YES);

  CGContextConcatCTM(theContext, shrinkingTransform);

  CGContextDrawPDFPage(theContext, pdfPage);  // draw the pdfPage into the bitmap context
  CGPDFDocumentRelease(pdfDocument);

  //
  // create the CGImageRef (and thence the UIImage) from the context (with its bitmap of the pdf page):
  //
  CGImageRef theCGImageRef = CGBitmapContextCreateImage(theContext);
  free(CGBitmapContextGetData(theContext));  // this frees the bitmapData we malloc'ed earlier
  CGContextRelease(theContext);

  UIImage *theUIImage;

  // CAUTION: the method imageWithCGImage:scale:orientation: only exists on iOS 4.0 or later!!!
  if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
  {
    theUIImage = [UIImage imageWithCGImage:theCGImageRef scale:pixelsPerPoint orientation:UIImageOrientationUp];
  }
  else
  {
    theUIImage = [UIImage imageWithCGImage:theCGImageRef];
  }

  CFRelease(theCGImageRef);
  return theUIImage;
}

You will need to supply a CGPDFDocumentRef corresponding to your PDF file, something like the following. (This one assumes that the file test.pdf exists in your app's main bundle.)

CGPDFDocumentRef MyGetPDFDocumentRef()
{
  NSString *inputPDFFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"test.pdf"];
  const char *inputPDFFileAsCString = [inputPDFFile cStringUsingEncoding:NSASCIIStringEncoding];
  //NSLog(@"expecting pdf file to exist at this pathname: "%s"", inputPDFFileAsCString);

  CFStringRef path = CFStringCreateWithCString(NULL, inputPDFFileAsCString, kCFStringEncodingUTF8);

  CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  CFRelease (path);

  CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);
  CFRelease(url);

  if (CGPDFDocumentGetNumberOfPages(document) == 0)
  {
    printf("Warning: No pages in pdf file "%s" or pdf file does not exist at this path
", inputPDFFileAsCString);
    return NULL;
  }

  return document;
}

Finally, you can display the thumbnail image in an UIImageView, like so:

  UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:[self buildThumbnailImage]];

  [self.view addSubview:thumbnailImageView];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...