首页 文章

在iphone中创建肖像pdf的缩略图

提问于
浏览
2

我是iPhone编程的新手 . 在我的应用程序中,我显示存储在我的资源文件夹中的PDF第一页的缩略图 . 我使用以下代码来创建缩略图,它适用于横向PDF . 但是在创建纵向PDF的缩略图时,我正在获得一些额外部分的图像,在webview中打开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而无需额外的部分 . 任何人都可以给我一个解决方案吗?

谢谢 .

1 回答

  • 0

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

    CGPDFPageRef page = CGPDFDocumentGetPage(pdf,1); // grab 第一个PDF页面CGRect aRect = CGPDFPageGetBoxRect(page,kCGPDFCropBox); //获取与框类型相关联的pdf页面的矩形(kCGPDFCropBox)

相关问题