问题描述
我想从UIView创建PDF。
现在我想复制该图层并将其调整为DIN A4页面大小。
I want to create a PDF from a UIView.Now I want to copy the layer and resize it to a DIN A4 page size.
CGRect A4PageRect = CGRectMake(0, 0, 595, 842);
CALayer *myLayer = [pdfView layer];
myLayer.bounds = pageRect;
但是此cody调整了屏幕上可见层的大小。
But this cody resizes the visible layer on my screen.
如何复制图层内容以调整其大小以适合A4页面?
How can I copy the layers contents to resize it to fit an A4 page?
感谢朱利安人的帮助
推荐答案
无法复制CALayer。
There is no way to duplicate a CALayer.
(对于CoreAnimation来说,以一种明智的方式来实现它是很困难的。可能会有一整层子层树,它们都可能有影响其行为的委托,
(It would be difficult for CoreAnimation to implement that in a sensible way. There might be a whole tree of sublayers, and they all might have delegates that influence their behavior, which wouldn't expect to suddenly get requests from the copies of the layers.)
我只能猜测找到一个更好的解决方案,因为我不了解您的确切情况。您是否有要调整大小的PDF,还是只想获取一个任意的现有图层并从中制作PDF文档?
I can only guess at a better solution, because I don't understand your exact situation. Do you have a PDF that you are trying to resize, or do you just want take an arbitrary existing layer and make a PDF document out of it?
如果是后者:
- 使用或创建PDF绘图上下文
- 调用或以创建页面
- 调用使用
- 缩放/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextScaleCTM rel = nofollow>
CGContextScaleCTM
因此您的图层将适合在PDF页面中 - 致电将图层绘制到PDF上下文中
- 致电
UIGraphicsEndPDFCo ntext
完成PDF
- Use
UIGraphicsBeginPDFContextToData
orUIGraphicsBeginPDFContextToFile
to create a PDF drawing context - Call
UIGraphicsBeginPDFPage
orUIGraphicsBeginPDFPageWithInfo
to create a page - Call
UIGraphicsGetCurrentContext
to get the PDF drawing context - Scale using
CGContextScaleCTM
so your layer will fit in the PDF page - Call
-[CALayer renderInContext:]
to draw the layer into the PDF context - Call
UIGraphicsEndPDFContext
to finish the PDF
请注意,这看起来很糟糕。图层是基于位图的,因此您将在PDF中获得位图。另外,-[CALayer renderInContext:]
的渲染与屏幕上的渲染并不完全相同-请参阅文档中的注释。
Note that this may look terrible. Layers are bitmap-based, so you'll get a bitmap in your PDF. Also, -[CALayer renderInContext:]
doesn't render exactly the same as it does on-screen -- see the note in the documentation.
如果出现问题,则需要添加一条绕过CALayer的单独绘制路径。在第5步中,您将使用CoreGraphics进行自己的绘制。
If this is a problem, you'll need to add a separate drawing path that bypasses CALayer. In step 5, you would do your own drawing using CoreGraphics.
这篇关于重复的CALayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!