Search code examples
ioscocoa-touchscreenshot

in app screenshot and load into email from different view controller


I want to take a screenshot of one of my view controller and load it into an email. The problem I am having is that I can take a screenshot of the current view controller but I wish to take the short of a separate view controller.

- (IBAction)showEmail:(id)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *example = UIImageJPEGRepresentation(image, 1.0);

// Email Subject
NSString *emailTitle = @"Test";
// Email Content
NSString *messageBody = @"Attached below is the snapshot of the view controller";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"];

//    UIImage *UIImageToSend;
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
//  NSData *data = UIImageJPEGRepresentation(UIImageToSend,1);

//UIImage *myImage = [UIImage imageNamed:@"example.png"];
//NSData *mageData = UIImagePNGRepresentation(myImage);

[mc addAttachmentData:example  mimeType:@"image/png" fileName:@"example.png"];
// 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];

}

I have tried changing the self in

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

to the other view controller class name but xcode then gives me an error that says

property of 'view' not found on object of type 'otherViewController'


Solution

  • you save jpeg data but declare a mime type of png. simple mismatch :)