0

我对 xcode 还很陌生,我目前正在尝试制作一个具有多个视图控制器的应用程序。我希望其中一个视图控制器以 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();

// 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);

}

然后另一个视图控制器具有以下代码,希望它将上面的快照加载到电子邮件的正文中,然后可以发送给任何人。

- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = @"Email Title";
// Email Content
NSString *messageBody = @"";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"test@hotmail.com"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

UIImage *myImage = [UIImage imageNamed:@"documentDirectoryFilename.pdf"];
NSData *imageData = UIImagePNGRepresentation(myImage);

[mc addAttachmentData:imageData  mimeType:@"image/pdf" fileName:@"documentDirectoryFilename.pdf"];

// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}

- (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: %@", [error localizedDescription]);
        break;
    default:
        break;
}

我遇到的问题是找不到文件并且没有成功加载到电子邮件中。任何帮助将不胜感激。

4

1 回答 1

0

嗨,我实际上解决了我遇到的问题,我忘记在视图控制器头文件中导入另一个视图控制器。很抱歉浪费了大家的时间。我认为这将是一个非常简单的解决方案。

于 2013-10-28T09:04:48.783 回答