【发布时间】:2016-04-20 04:22:44
【问题描述】:
我有一个 scrollView 有多个子视图,如详细发票。我想将我的scrollView 转换为 pdf,以便我可以轻松地将其发送到打印机以及通过电子邮件发送。我们应该怎么做?有什么可能或简单的方法吗?
【问题讨论】:
标签: ios objective-c uiview uiscrollview
我有一个 scrollView 有多个子视图,如详细发票。我想将我的scrollView 转换为 pdf,以便我可以轻松地将其发送到打印机以及通过电子邮件发送。我们应该怎么做?有什么可能或简单的方法吗?
【问题讨论】:
标签: ios objective-c uiview uiscrollview
您可以使用Uiview 来执行此操作。因此,首先您应该在滚动视图中添加 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();
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];
documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
调用这个方法,传递uiview(你想从中制作pdf),字符串表示文件名。此方法将创建 pdf 并将数据存储到文档目录。如果您不想存储,请评论该部分。
你可以显示pdfdata到webview喜欢,
[webview loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
希望这会有所帮助:)
【讨论】: