iOS:从UIView创建PDF [英] iOS: Create PDF from UIView

查看:118
本文介绍了iOS:从UIView创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 UIView 创建PDF,其中可能包含图片,标签等。
我已检查参考( GeneratingPDF )我也知道 renderInContext:方法,可用于将 UIView 的内容绘制到PDF上下文中。

I am trying to create a PDF from a UIView which may contain images, labels, etc. I have checked the reference ( GeneratingPDF) and I am also aware of the renderInContext: method, which can be used to draw the UIView's content onto the PDF context.

我使用 renderInContext:来创建PDF。但是,图像和文字失去了保真度。即图像不能单独选择和放大。在生成的PDF中无法选择/复制文本等。它与从 UIView 的快照创建的PDF一样好/坏。

I used renderInContext: to create a PDF. However, the images and the text lost their fidelity. i.e. The images were not selectable individually & the text could not be selected/copied, etc. in the resulting PDF. It was as good/bad as the PDF created from a snapshot of the UIView.

我的问题是,做我必须画出文字和单独的图像来实现我想要的(使用 CGContextShowTextAtPoint & CGContextDrawImage )?

My question is, do I have to draw the texts & images individually to achieve what I want (using CGContextShowTextAtPoint & CGContextDrawImage)?

如果是,那么如何扫描 UIView 获取文本&图片?假设我从外部收到 UIView &我不知道它的内容。

If yes, then how do I go about scanning the UIView for texts & images? Assume that I receive the UIView from outside & am not aware of its contents.

推荐答案

你确实必须将文本/图像单独绘制成 CGContextRef (查看 CGPDFContextCreateWithURL )使用 CGContextShowTextAtPoint 等函数。虽然理论上你可以扫描UIView的图像/标签,但如果你只是从头开始绘制布局会更好。您必须有一些方法来了解UIView中应该包含哪些元素。虽然代码会有很大不同,但从逻辑上讲,你应该以与编程方式绘制UIView相同的方式来接近它,而不是从笔尖加载它。

You do indeed have to draw the texts/images individually into a CGContextRef (look at CGPDFContextCreateWithURL) using functions like CGContextShowTextAtPoint. While you could theoretically scan your UIView for images/labels, it would be better if you just draw the layout from scratch. You must have some way to know what elements should be in the UIView. Although the code will be very different, logically you should approach it the same way you would if you were going to draw the UIView programmatically instead of loading it from a nib.

如果你真的想要扫描你的UIView,你可以这样做:

If you really want to scan your UIView, you could do something like this:

for(UIView *subview in parentView.subviews) {
 if([subview isKindOfClass:[UIImageView class]]) {
  ...
 } else if([subview isKindOfClass:[UITextView class]]) {
  ...
 } else if([subview isKindOfClass:[UILabel class]]) {
  ...
 }
}

这篇关于iOS:从UIView创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆