我目前正在创建和保存 UIView 的 PDF。它工作得很好,但现在我正在尝试从保存到磁盘而不是 UIView 的文本数据创建 PDF。理想情况下,我想根据 UITextView 中的文本创建 PDF。下面的代码可以很好地编写文本视图的第一部分,但它不会从整个文本视图创建 PDF。这是相当多的文本,因此它仅根据视图中适合的文本量创建 PDF。我目前使用的代码是这样的:
- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{
NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
// 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, textview.bounds, myDictionary);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[textview.layer renderInContext:pdfContext]; // this line
// 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:@"filename"];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
为了调用它/保存它,我将此消息发送到 IBAction 中:[self createPDFfromUIView:textview saveToDocumentsWithFileName:@"pdflocation"];
现在,我已经解析这段代码和文档几个小时了,无法弄清楚如何根据保存的文本文件而不是视图来创建 PDF。我还是编程新手,所以很多事情都超出了我的想象。有谁知道如何更改上述代码以使其基于保存的文本而不是视图创建 PDF?
选项:我可以根据 NSString 的输出创建 PDF。我也可以将保存的文本放入 UITextView 并根据该(原始计划)创建 PDF,但我也不知道该怎么做。