Как сделать снимок экрана и отправить его по электронной почте? - PullRequest
11 голосов
/ 14 июля 2011

Могу ли я заставить мое приложение сделать снимок экрана с содержимым представления и прикрепить его к электронному письму? Как?

Ответы [ 5 ]

14 голосов
/ 14 июля 2011

Вы можете преобразовать свой вид в изображение, затем вы можете создать электронное письмо с ним.

Этот код ( отсюда ) позволит вам отправить электронное письмо с вложением:

    - (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];
     }

Чтобы преобразовать графическое представление вашего вида в изображение, используйте код ( отсюда ):

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

[self emailImageWithImageData:data];
1 голос
/ 19 июля 2013

Да, пожалуйста, проверьте, может ли вам помочь приведенный ниже код

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"];
1 голос
/ 14 июля 2011

С этого сайта :

 // 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];
0 голосов
/ 08 сентября 2014

Попробуйте таким образом

Поместите этот почтовый код в действие

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];

}

Вот метод скриншота

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

Беги и иди!

N.B. Не забудьте импортировать заголовок #import <MessageUI/MFMailComposeViewController.h>

0 голосов
/ 19 апреля 2012

Вот учебник для этого.Вы можете найти исходный код на github этого как wwll.

Сделайте снимок экрана текущего представления и прикрепите его к почте

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...