11

Can I make my app take a screenshot of the contents of a view and attach it to an email? How?

sergio
  • 68,819
  • 11
  • 102
  • 123
Randall
  • 2,083
  • 2
  • 18
  • 20
  • 1
    You can take a look at [`this`](http://stackoverflow.com/questions/6450303/in-app-screenshot-and-attach-to-email-without-saving-into-library/6450665#6450665). – Deepak Danduprolu Jul 13 '11 at 20:29

5 Answers5

14

You can convert your view to an image, then you could create an email with it.

This code (from here) will allow you to send an email with an attachment:

    - (void)emailImageWithImageData:(NSData *)data
    {
      MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
      picker.mailComposeDelegate = self;

      // Set the subject of email
      [picker setSubject:@"Picture from my iPhone!"];

      // Add email addresses
      // Notice three sections: "to" "cc" and "bcc" 
      [picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", @"emailaddress2@domainName.com", nil]];
      [picker setCcRecipients:[NSArray arrayWithObject:@"emailaddress3@domainName.com"]];   
      [picker setBccRecipients:[NSArray arrayWithObject:@"emailaddress4@domainName.com"]];

      //    Fill out the email body text
      NSString *emailBody = @"I just took this picture, check it out.";

      // This is not an HTML formatted email
      [picker setMessageBody:emailBody isHTML:NO];

      // Attach image data to the email
      // 'CameraImage.png' is the file name that will be attached to the email
      [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];

      // Show email view    
      [self presentModalViewController:picker animated:YES];
      //if you have a navigation controller: use that to present, else the user will not
      //be able to tap the send/cancel buttons
      //[self.navigationController presentModalViewController:picker animated:YES];


      // Release picker
      [picker release];
    }

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
     {
       // Called once the email is sent
       // Remove the email view controller  
       [self dismissModalViewControllerAnimated:YES];
     }

To convert your view graphical representation to an image, use the code (from here):

UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);

[self emailImageWithImageData:data];
Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I must agree, something about this answer is beautiful. It is right to the point, exactly what I was looking for! – Alex Stone Nov 21 '11 at 21:40
1

Yes you can please check the below code may help you

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* image = [GMSScreenshot screenshotOfMainScreen];
UIGraphicsEndImageContext();
data = UIImagePNGRepresentation(image);
[picker addAttachmentData:data mimeType:@"image/jpg" fileName:@"Screenshot"];
sarit bahuguna
  • 875
  • 8
  • 8
1

From this site:

 // CREATING MAIL VIEW
 MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
 controller.mailComposeDelegate = self;
 [controller setSubject:@"Check this route out"];
 [controller setMessageBody:@"Attaching a shot of covered route." isHTML:NO];

 // MAKING A SCREENSHOT
 UIGraphicsBeginImageContext(_mapView.frame.size);
 [_mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 // ATTACHING A SCREENSHOT
 NSData *myData = UIImagePNGRepresentation(screenshot);
 [controller addAttachmentData:myData mimeType:@"image/png" fileName:@"route"]; 

 // SHOWING MAIL VIEW
 [self presentModalViewController:controller animated:YES];
 [controller release];
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
0

Here is a tutorial for this. You can find the source code on github of this as wwll.

Take Screen-shot of Current View and Attach it to Mail

Hope it helps.

Akshay
  • 2,973
  • 6
  • 43
  • 75
0

Try This way

Put this mail code inside an Action

if([MFMailComposeViewController canSendMail]){


  MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
  picker.mailComposeDelegate = self;
  [picker setSubject:@"Test Screen Shot From app"];
  [picker addAttachmentData: UIImagePNGRepresentation([self screenshot]) mimeType:@"image/png" fileName:@"CameraImage.png"];

  [self presentViewController:picker animated:YES completion:nil];

}

Here is the screenshot method

- (UIImage *) screenshot {
  UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

  [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return image;
}

Run and Go!

N.B.Don't forget to Import header #import <MessageUI/MFMailComposeViewController.h>

Tunvir Rahman Tusher
  • 6,421
  • 2
  • 37
  • 32