【问题标题】:Convert PDF to UIImageView将 PDF 转换为 UIImageView
【发布时间】:2012-01-19 09:17:02
【问题描述】:

我找到了一些代码,它从 PDF 文件中给了我一个UIImage。它有效,但我有两个问题:

  • 是否有可能获得更好的 UIImage 质量? (见截图)
  • 我只看到UIImageView 的第一页。我是否必须将文件嵌入到 UIScrollView 中才能完成?
  • 还是只呈现一个页面并使用按钮浏览页面更好?

附:我知道UIWebView 可以显示具有某些功能的PDF 页面,但我需要它作为UIImage 或至少在UIView 中。

Bad quality Image:

Code:

-(UIImage *)image {
UIGraphicsBeginImageContext(CGSizeMake(280, 320)); 

CGContextRef context = UIGraphicsGetCurrentContext();

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("ls.pdf"), NULL, NULL);

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

CGContextTranslateCTM(context, 0.0, 320);

CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 4);

CGContextSaveGState(context);

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true);

CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();
return resultingImage;
}

【问题讨论】:

    标签: ios xcode image pdf


    【解决方案1】:

    你在用CGContextTranslateCTM(context, 0.0, 320); 电话做什么?

    您应该从 pdf 中提取正确的指标,代码如下:

     cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
     rotate = CGPDFPageGetRotationAngle(page);
    

    另外,如您所见,pdf 可能包含旋转信息,因此您需要根据角度使用 CGContextTranslateCTM/CGContextRotateCTM/CGContextScaleCTM

    您还可能想要剪辑CropBox 区域之外的任何内容,因为 pdf 有各种您通常不想显示的viewPorts(例如,对于打印机,以便可以进行无缝打印)-> 使用 @ 987654326@.

    接下来,您忘记了 pdf 参考定义了白色背景色。有很多文档根本没有定义任何背景颜色 - 如果你不自己绘制白色背景,你会得到奇怪的结果 --> CGContextSetRGBFillColor & CGContextFillRect

    【讨论】:

      【解决方案2】:

      我知道我在这里有点晚了,但我希望我可以帮助其他人寻找答案。 至于提出的问题:

      • 恐怕获得更好图像质量的唯一方法是渲染更大的图像,并让UIImageView 为您调整大小。我认为您无法设置分辨率,但使用更大的图像可能是一个不错的选择。页面渲染不会花费太长时间,并且图像将具有更好的质量。 PDF 文件根据缩放级别按需呈现,这就是为什么它们似乎具有“更好的质量”。

      • 至于渲染所有页面,您可以调用CGPDFDocumentGetNumberOfPages( pdf ) 获取文档中的页面数,并使用简单的for 循环您可以将生成的所有图像连接到一张图像中。要显示它,请使用UIScrollVIew

      • 在我看来,这种方法比上面的方法要好,但是你应该尝试优化它,例如总是渲染当前、上一页和下一页。对于漂亮的滚动过渡效果,为什么不使用水平的UIScrollView

      对于更通用的渲染代码,我总是这样旋转:

      int rotation = CGPDFPageGetRotationAngle(page);
      
      CGContextTranslateCTM(context, 0, imageSize.height);//moves up Height
      CGContextScaleCTM(context, 1.0, -1.0);//flips horizontally down
      CGContextRotateCTM(context, -rotation*M_PI/180);//rotates the pdf
      CGRect placement = CGContextGetClipBoundingBox(context);//get the flip's placement
      CGContextTranslateCTM(context, placement.origin.x, placement.origin.y);//moves the the correct place
      
      //do all your drawings
      CGContextDrawPDFPage(context, page);
      
      //undo the rotations/scaling/translations
      CGContextTranslateCTM(context, -placement.origin.x, -placement.origin.y);
      CGContextRotateCTM(context, rotation*M_PI/180);
      CGContextScaleCTM(context, 1.0, -1.0);
      CGContextTranslateCTM(context, 0, -imageSize.height);
      

      Steipete 已经提到设置白色背景:

      CGContextSetRGBFillColor(context, 1, 1, 1, 1);
      CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height)); 
      

      所以最后要记住的是,在导出图像时,将质量设置为最高。例如:

      UIImageJPEGRepresentation(image, 1);
      

      【讨论】:

        猜你喜欢
        • 2017-10-10
        • 2012-07-15
        • 2013-07-07
        • 2016-03-17
        • 2016-06-15
        • 1970-01-01
        • 2012-05-06
        • 2016-10-18
        • 2013-06-29
        相关资源
        最近更新 更多