【问题标题】:How to take a screenshot of a view and email it?如何截取视图并通过电子邮件发送?
【发布时间】:2011-10-04 20:06:10
【问题描述】:

我可以让我的应用截取视图内容并将其附加到电子邮件中吗?怎么样?

【问题讨论】:

  • 你可以看看this

标签: iphone objective-c ios uiview


【解决方案1】:

您可以将视图转换为图像,然后您可以使用它创建电子邮件。

此代码 (from here) 将允许您发送带有附件的电子邮件:

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

要将您的视图图形表示转换为图像,请使用代码 (from here):

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

[self emailImageWithImageData:data];

【讨论】:

  • 我必须同意,这个答案很漂亮。切中要害,正是我想要的!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    是的,你可以请检查下面的代码可能对你有帮助

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

    【讨论】:

    • 它会截取当前视图的屏幕截图,然后我们将其用作图像。并在 mfmailcomposer 中使用.....
    【解决方案4】:

    这是一个教程。你可以在 github 上找到 wwll 的源代码。

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

    希望对你有帮助。

    【讨论】:

      【解决方案5】:

      试试这个方法

      将此邮件代码放入 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];
      
      }
      

      这里是截图方法

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

      奔跑吧!

      注意不要忘记导入标头#import <MessageUI/MFMailComposeViewController.h>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-04
        • 2014-03-22
        • 2012-10-24
        • 1970-01-01
        • 2018-09-20
        相关资源
        最近更新 更多