ios - 如何将整个 tableview 捕获为图像,从中创建 .pdf 并通过电子邮件发送
<p><p>这是我第一次尝试在 iOS 中创建 .pdf 文件。
我有一个表格 View ,可以生成我想要在 .pdf 文件中呈现的所有数据。</p>
<p>这是我将整个表格捕获为图像、从图像生成 pdf 并通过电子邮件发送的代码:</p>
<pre><code>- (IBAction)save:(id)sender {
// save all table
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
UIGraphicsBeginImageContextWithOptions(self.tableView.bounds.size, self.tableView.opaque, 0.0);
;
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageData = UIImagePNGRepresentation(saveImage);
UIImage *image = ;
UIImageView *imageView = [initWithImage:image];
;
}
- (NSMutableData*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = ;
// 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
;
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = ;
NSString* documentDirectoryFilename = ;
// instructs the mutable data object to write its context to a file on disk
;
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
return pdfData;
}
-(IBAction)back:(id)sender {
;
}
-(IBAction)email:(id)sender{
MFMailComposeViewController *mc = [ init];
mc.mailComposeDelegate = self;
UIImage *image = ;
UIImageView *imageView = [initWithImage:image];
NSMutableData *pdfData = ;
// Attach an image to the email
;
// Present mail view controller on screen
;
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", );
break;
default:
break;
}
// Close the Mail Interface
;
}
</code></pre>
<p>我已经测试了imageData并且捕获成功。生成pdf,但它是单页。 </p>
<p>期望的结果是 <code>imageData</code> 捕获的图像用于创建多页 pdf。</p>
<p>我应该如何调整'createPDFfromUIView'方法以使用A4标准纸将长图像文件分成多页。</p>
<p>任何帮助将不胜感激</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个而不是 <code>UIGraphicsBeginImageContextWithOptions</code></p>
<pre><code> CGRect priorBounds = self.tableView.bounds;
CGSize fittedSize = ;
self.tableView.bounds = CGRectMake(0, 0, fittedSize.width, fittedSize.height);
CGRect pdfPageBounds = CGRectMake(0, 0, 612, 792); // Change this as your need
NSMutableData *pdfData = [ init];
UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); {
for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height) {
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil);
CGContextSaveGState(UIGraphicsGetCurrentContext()); {
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
;
} CGContextRestoreGState(UIGraphicsGetCurrentContext());
}
} UIGraphicsEndPDFContext();
self.tableView.bounds = priorBounds; // Reset the tableView
// Use the pdfData to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = ; //Get the docs directory
filePathPDF = ; //Add the file name
BOOL written = ;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何将整个 tableview 捕获为图像,从中创建 .pdf 并通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24425271/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24425271/
</a>
</p>
页:
[1]