如何在 iOS 中将 UIView 转换为 PDF? [英] How to Convert UIView to PDF within iOS?
问题描述
有很多关于如何在应用程序的UIView
中显示 PDF 的资源.我现在正在做的是从 UIViews
创建一个 PDF.
There are a lot of resources about how to display a PDF in an App's UIView
. What I am working on now is to create a PDF from UIViews
.
例如,我有一个 UIView
,带有 Textviews、UILabels
、UIImages
等子视图,那么我如何转换 big UIView
作为一个整体,包括它的所有子视图和子视图到 PDF?
For example, I have a UIView
, with subviews like Textviews, UILabels
, UIImages
, so how can I convert a big UIView
as a whole including all its subviews and subsubviews to a PDF?
我检查了 Apple 的 iOS 参考一>.但是,它只讨论将文本/图像片段写入 PDF 文件.
I have checked Apple's iOS reference. However, it only talks about writing pieces of text/image to a PDF file.
我面临的问题是我想以 PDF 格式写入文件的内容很多.如果我将它们逐个写入 PDF,那将是一项艰巨的工作.这就是为什么我正在寻找一种将 UIViews
写入 PDF 甚至位图的方法.
The problem I am facing is that the content I want to write to a file as PDF is a lot. If I write them to the PDF piece by piece, it is going to be huge work to do.
That's why I am looking for a way to write UIViews
to PDFs or even bitmaps.
我尝试了从 Stack Overflow 中的其他问答中复制的源代码.但它只给了我一个带有 UIView
边界大小的空白 PDF.
I have tried the source code I copied from other Q/A within Stack Overflow. But it only gives me a blank PDF with the UIView
bounds size.
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView drawRect:aView.bounds];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
推荐答案
注意下面的方法创建只是一个位图的视图;它不会创建实际的排版.
Note that the following method creates just a bitmap of the view; it does not create actual typography.
(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
还要确保您导入:QuartzCore/QuartzCore.h
Also make sure you import: QuartzCore/QuartzCore.h
这篇关于如何在 iOS 中将 UIView 转换为 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!