【发布时间】:2011-07-23 12:35:54
【问题描述】:
有很多关于如何在应用程序的UIView 中显示 PDF 的资源。我现在正在做的是从UIViews 创建一个 PDF。
例如,我有一个UIView,有像 Textviews、UILabels、UIImages 这样的子视图,那么如何将一个 big UIView 转换为一个整体,包括它的所有子视图以及 PDF 的子视图?
我已经检查了Apple's iOS reference。但是,它只涉及将文本/图像写入 PDF 文件。
我面临的问题是我想以 PDF 格式写入文件的内容很多。如果我将它们逐个写入 PDF,这将是一项巨大的工作。
这就是为什么我正在寻找一种将UIViews 写入 PDF 甚至位图的方法。
我已经尝试过从 Stack Overflow 的其他问答中复制的源代码。但它只给了我一个空白 PDF,其边界大小为 UIView。
-(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);
}
【问题讨论】:
标签: ios pdf uiview uiimage core-graphics