• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 PDF 转换为 UIImageView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:09:08 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

劣质图片:

Bad quality Image

代码:

-(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;
}



Best Answer-推荐答案


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

  • 恐怕要获得更好的图像质量,唯一的方法就是渲染更大的图像,然后让 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);

关于ios - 将 PDF 转换为 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8490238/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap