编辑PDF并在UIWebView中显示图像时,缩放比例不合适 [英] Inappropriate scaling factor when editing PDF and displaying image in UIWebView
问题描述
我正在努力地进行PDF编辑,所以我从朴素的代码开始尝试实现以下目标:
I'm struggling with PDF editing so I'm starting with naive code trying to achieve the following :
- 显示与应用程序捆绑在一起的PDF
- 单击要添加图像的屏幕(例如签名)
- 使用编辑的PDF重新加载Web视图
这是我在github上的代码: https://github.com/HRiffiod/PDFSignature.git
Here is my code on github : https://github.com/HRiffiod/PDFSignature.git
问题是我的图像未显示在编辑的PDF中的正确位置.它不是随机的,但是我离(0,0)坐标越远,偏差越大,这就是为什么我押错了缩放比例的原因.
The problem is that my image does not display at the right position in the edited PDF. It is not random, but the farther I get from the (0,0) coordinates, the bigger the deviation, that's why I'm betting on a wrong scaling factor.
例如,如果我点击第一个单词,然后单击签名!",这就是我得到的. :
For example, this is what I get if I tap the first word and then click "Sign !" :
所以这很准确.但是,如果我单击此PDF页面上的最后一个单词(即我的意思是页面,而不是本段),则会发生这种情况:
So this is pretty accurate. however, this is what happens if I click the last word from this PDF page (and I mean page, not this paragraph) :
那根本不行.
有什么建议吗? 预先谢谢你
Any suggestion ? Thank you in advance
编辑
这是一段代码(您可以在github上找到),上下文渲染将在其中启动,并且可能是我做错了的地方:
Here is a piece of code (that you can find on github) where the context rendering kicks in and might be the place I am doing it wrong :
// loop over PDF pages to render it
for(size_t page = 1; page <= numberOfPages; page++)
{
// Get the current page and page frame
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
// Draw the page (flipped)
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
// Get appropriate scale
//CGFloat scale = [[UIScreen mainScreen] scale];
// As suggested in SO, the (1, -1) scale might need to be changed but couldn't get it right. Y axis is negative in order to render PDF (mandatory)
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
CGContextDrawPDFPage(ctx, pdfPage);
CGContextRestoreGState(ctx);
if(page == 1)
{
[self drawLogo];
}
}
UIGraphicsEndPDFContext();
推荐答案
您似乎没有为视网膜设备使用适当的比例尺.
It looks like you are not using the appropriate scale for retina devices.
ViewController.m
ViewController.m
CGContextScaleCTM(ctx, 1, -1);
这应该基于设备的规模.
This should be based off the device's scale.
例如(假设y比例值出于某种原因需要为负)
e.g. (Assuming the y scale value needs to be negative for some reason)
CGFloat scale = [[UIScreen mainScreen] scale];
CGContextScaleCTM(ctx, scale, -scale);
这篇关于编辑PDF并在UIWebView中显示图像时,缩放比例不合适的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!