有很多关于如何在App‘sUIView
中显示PDF的资源.我现在的工作是从UIViews
创建一个PDF.
例如,我有一个UIView
,带有文本视图、UILabels
、UIImages
等子视图,那么如何将big UIView
作为一个整体,包括其所有子视图和子视图转换为PDF?
我已经查了Apple's iOS reference张了.然而,它只谈到将文本/图像片段写入PDF文件.
我面临的问题是,我想要以PDF格式写入文件的内容很多.如果我把它们一块一块地写到PDF中,那将是一项巨大的工作.
这就是为什么我正在寻找一种方法来写入UIViews
到PDF,甚至位图.
我已经try 了从Stack Overflow中的其他Q/A复制的源代码.但它只给我一个UIView
边界大小的空白PDF.
-(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);
}