从 CGPDfDocument 页面保存图像并不完全适合 UIImageview

发布于 2024-12-18 17:02:32 字数 1443 浏览 5 评论 0原文

我在将 PDF 页面保存为 UIImage 时遇到一些问题...pdf 是从互联网加载的,它有一页(原始 PDF 已在服务器中分割)...但转换后的图像有时会被裁剪...有时它很小,当它放在 UIImageview 上时会留下空白...

这是代码

-(UIImage *)imageFromPdf:(NSString *) pdfUrl{

NSURL *pdfUrlStr=[NSURL URLWithString:pdfUrl];

CFURLRef docURLRef=(CFURLRef)pdfUrlStr;


UIGraphicsBeginImageContext(CGSizeMake(768, 1024)); //840, 960
NSLog(@"save begin");

CGContextRef context = UIGraphicsGetCurrentContext();

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

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(docURLRef);

NSLog(@"save complete");

CGContextTranslateCTM(context, 0.0, 900);//320

CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);

CGContextSaveGState(context);


CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 768, 1024), 0, true);

CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);

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

return resultingImage;

}

顺便说一句,我已经通过这样的编码准备了我的 UIImageview

self.PDFImageVIew.contentMode  = UIViewContentModeScaleAspectFit;
self.PDFImageVIew.clipsToBounds = YES;

我只是希望这个图像完美地适合 UIImageview ,并且可能会降低图像质量。 ..你可以吗建议我怎样才能保持质量?请帮助并给我一些建议,

谢谢

I am having some trouble with saving a PDF page as UIImage...the pdf is loaded from the internet and it has one page(original PDF has been splitted in sever)...but the converted image sometimes is cropped...sometimes it is small and leave white space when it is putted on UIImageview...

here is the code

-(UIImage *)imageFromPdf:(NSString *) pdfUrl{

NSURL *pdfUrlStr=[NSURL URLWithString:pdfUrl];

CFURLRef docURLRef=(CFURLRef)pdfUrlStr;


UIGraphicsBeginImageContext(CGSizeMake(768, 1024)); //840, 960
NSLog(@"save begin");

CGContextRef context = UIGraphicsGetCurrentContext();

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

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL(docURLRef);

NSLog(@"save complete");

CGContextTranslateCTM(context, 0.0, 900);//320

CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);

CGContextSaveGState(context);


CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 768, 1024), 0, true);

CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);

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

return resultingImage;

}

btw I have prepared my UIImageview by coding like this

self.PDFImageVIew.contentMode  = UIViewContentModeScaleAspectFit;
self.PDFImageVIew.clipsToBounds = YES;

I just want this image perfectly fitted on UIImageview and may be its reducing the quality of image...can you have suggesion how can I keep the quality also? please help and give me some suggestion

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-12-25 17:02:32
CGContextTranslateCTM(context, 0.0, 900);//320
  • 这里通常翻译操作的最后一个参数应该是上下文的高度或您为其创建图像的矩形的高度。所以,我认为它应该是 1024(您已经将图像上下文的高度设置为 1024,所以这里我假设状态栏不存在)。这可以消除裁剪问题。我在您的代码中注意到的其他一些事情您应该必须在对上下文进行任何操作之前保存图形的状态。您正在保存它,但经过几次操作后。
  • 上面的代码将尝试使其高度适合,因此如果实际页面的高度大于上下文高度,那么它将按比例缩小。所以你可以明显地看到页面周围的空白。
  • 另一件事是,如果你的原始 pdf 页面中有空白,那么据我所知,没有办法消除它。
CGContextTranslateCTM(context, 0.0, 900);//320
  • Here generally last parameter of translate operation should be the height of context or height of rectangle for which you creating image. So, i think it should be 1024(You have taken height of image context is 1024 so here i am assuming that status bar is not present). This may eliminate the issue of cropping. Some more things that i have noted on your code you should have to save the state of graphics before any operation on context. You have are saving it but after few operations.
  • Above code will try to make it height fit so if height of actual page is bigger than your context height then it will be scaled down. so you can obviously see white space around page.
  • One more thing if your original pdf page have white space in it then there is no way to eliminate it as far as i know.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文