捕获屏幕截图并将其发送到电子邮件正文中

时间:2014-07-24 11:14:41

标签: objective-c email screenshot

我正在尝试捕获屏幕截图并将该图像发送到电子邮件中,最好是在正文中 这是我的代码,但它不起作用:(

- (IBAction) Btn_Clicked: (id) sender
{
    //take the screenshot
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData* imageData = UIImageJPEGRepresentation(screenshotImage, 1);

    //prepare the email form
    NSString *emailTitle = MESSAGE_EMAIL_TTITLE;
    NSString *messageBody = MESSAGE_EMAIL_TEXT;

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

    // Add attachment
    [mc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:nil];

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

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

1 个答案:

答案 0 :(得分:1)

替换

[mc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:nil];

[mc addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"screenshot.jpg"];