【问题标题】:Creating thumbnail of a portrait pdf in iphone在iphone中创建肖像pdf的缩略图
【发布时间】:2012-06-12 15:26:09
【问题描述】:

我是 iPhone 编程新手。在我的应用程序中,我显示了存储在我的资源文件夹中的 PDF 第一页的缩略图。我正在使用以下代码创建缩略图,它适用于横向 PDF。但是在创建纵向 PDF 的缩略图时,我得到了一个带有一些额外部分的图像,在 web 视图中打开 PDF 时我看不到这些部分。

这是我用来创建缩略图的代码:

NSURL* pdfFileUrl=[NSURLfileURLWithPath:filePath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);

            CGRect aRect = CGRectMake(0, 0, 1024, 768);
            UIGraphicsBeginImageContext(aRect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();                    

            CGContextSaveGState(context);                
            CGContextTranslateCTM(context, 0.0, aRect.size.height);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, -(aRect.origin.x), -(aRect.origin.y));

            CGContextSetGrayFillColor(context, 1.0, 1.0);
            CGContextFillRect(context, aRect);            

            //Grab the first PDF page
            CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, aRect, 0, false);
            // And apply the transform.
            CGContextConcatCTM(context, pdfTransform);            
            CGContextDrawPDFPage(context, page);

            // Create the new UIImage from the context
            UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();

            CGContextRestoreGState(context);          
            UIGraphicsEndImageContext();    
            CGPDFDocumentRelease(pdf);

在使用此代码创建纵向 PDF 的缩略图图像时,我得到的图像不是确切的图像。它有一些额外的部分。我认为,这是该 PDF 的一些隐藏部分。此外,当我在 UIWebView 中加载相同的 PDF 时,我可以查看正确的 PDF 而无需额外的部分。谁能给我一个解决方案?

谢谢。

【问题讨论】:

    标签: ios pdf


    【解决方案1】:

    我发现问题在于采用 CGRect。我用 iPad 对齐对矩形进行了硬编码。实际上,我们必须采用我们正在使用的 PDF 页面的矩形。所以我在我的代码中使用了以下更改来制作正确的 PDF 缩略图。

    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);//抓取第一个PDF页面

    CGRect aRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);//获取与box类型关联的pdf页面的矩形(kCGPDFCropBox)

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 1970-01-01
      • 2011-02-20
      • 2017-06-04
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多