以下是我用于我的应用程序的内容。这在iOS 7中工作正常,但在iOS 8中崩溃。
使用此方法,我会截取屏幕截图并创建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();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:30.0/255.0 green:172.0/255.0 blue:254.0/255.0 alpha:1]];
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Payment Receipt"];
[mailer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"%@",aFilename]];
NSString *emailBody = @"Payment Receipt";
[mailer setMessageBody:emailBody isHTML:NO];
[[mailer navigationBar] setTintColor:[UIColor whiteColor]];
[self presentViewController:mailer animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email error!"
message:@" You do not have an email address configured in your device"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
当我添加Exception断点时,我可以看到它停在UIGraphicsEndPDFContext()
过去3天一直试图找到解决方案但没有成功。
答案 0 :(得分:1)
这是我使用的,它的工作原理。
-(NSData *) createPDFDataFrom:(UIView *) aView
{
// 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();
return pdfData;
}
然后在代码中的另一个位置:
//创建电子邮件地址
MFMailComposeViewController * mailVC = [[MFMailComposeViewController alloc] init];
mailVC.mailComposeDelegate = self;
[mailVC setSubject:[NSString stringWithFormat:@"%@: %@",@"Report",self.graphicContainer.graphicContainerName]];
[mailVC addAttachmentData:[self createPDFDataFrom:self.baseView] mimeType:@"pdf" fileName:[NSString stringWithFormat:@"%@.pdf",@"Report"]];
[self presentViewController:mailVC animated:YES completion:nil];
[mailVC release];