查看控制器屏幕拍摄

时间:2013-10-27 16:59:52

标签: user-interface uiviewcontroller xcode-storyboard

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

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

1 个答案:

答案 0 :(得分:0)

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