菜鸟教程小白 发表于 2022-12-13 10:09:08

ios - 将 PDF 转换为 UIImageView


                                            <p><p>我找到了一些代码,可以从 PDF 文件中获得 <code>UIImage</code>。它有效,但我有两个问题:</p>

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

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

<p><code>劣质图片:</code></p>

<p> <img src="/image/DESfc.png" alt="Bad quality Image"/> </p>

<p><code>代码:</code></p>

<pre><code>-(UIImage *)image {
UIGraphicsBeginImageContext(CGSizeMake(280, 320));

CGContextRef context = UIGraphicsGetCurrentContext();

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR(&#34;ls.pdf&#34;), 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;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我知道我在这里有点晚了,但我希望我可以帮助其他人寻找答案。
至于提出的问题:</p>

<ul>
<li><p>恐怕要获得更好的图像质量,唯一的方法就是渲染更大的图像,然后让 <code>UIImageView</code> 为你调整它的大小。我认为您无法设置分辨率,但使用更大的图像可能是一个不错的选择。页面渲染时间不会太长,图像质量会更好。 PDF 文件根据缩放级别按需呈现,这就是它们看起来“质量更好”的原因。</p></li>
<li><p>至于渲染所有页面,您可以调用 <code>CGPDFDocumentGetNumberOfPages( pdf )</code> 并使用简单的 <code>for</code> 循环来获取文档中的页面数将生成的所有图像连接到一张图像中。要显示它,请使用 <code>UIScrollVIew</code>。</p></li>
<li><p>在我看来,这种方法比上面的方法要好,但是你应该尝试优化它,例如总是渲染当前页面、上一个页面和下一个页面。对于漂亮的滚动过渡效果,为什么不使用水平 <code>UIScrollView</code>。</p></li>
</ul>

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

<pre><code>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&#39;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);
</code></pre>

<p>Steipete 已经提到设置白色背景:</p>

<pre><code>CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
</code></pre>

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

<pre><code>UIImageJPEGRepresentation(image, 1);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 PDF 转换为 UIImageView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8490238/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8490238/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 PDF 转换为 UIImageView