User interface 视图控制器屏幕截图

User interface 视图控制器屏幕截图,user-interface,uiviewcontroller,xcode-storyboard,User Interface,Uiviewcontroller,Xcode Storyboard,我对xcode相当陌生,我目前正在尝试制作一个具有多个视图控制器的应用程序。我希望其中一个视图控制器将另一个的PDF格式图像(屏幕快照)加载到电子邮件中 因此,一个视图控制器具有以下代码来拍摄视图的快照 - (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { // Creates a mutable data object for updating with bin

我对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;
}

我遇到的问题是没有找到该文件,也没有成功地将其加载到电子邮件中。任何帮助都将不胜感激。

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