应用程序内截图并附加到电子邮件IOS

时间:2013-11-23 13:43:27

标签: ios objective-c email screenshot

我想知道代码来截取屏幕截图并打开电子邮件窗口,只需按下应用程序中的一个按钮即可将屏幕截图附加到电子邮件中。到目前为止,我已经编写了代码,只需按一下按钮即可显示电子邮件窗口,我希望使用相同的按钮来附加屏幕截图。如果你能指出我正确的方向,将非常感激。

- (IBAction)showEmail:(id)sender {
    NSString *emailTitle = @"Test Email";
    NSString *messageBody = @"CHECK OUT MY NEW SCORE!";
    NSArray *toRecipents = [NSArray arrayWithObject:@"example@gmail.com"];

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

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];
}

2 个答案:

答案 0 :(得分:4)

MFMailComposeViewController类具有此功能。

-(void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename

这将获得屏幕截图

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
     UIGraphicsBeginImageContext(self.window.bounds.size);
 [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 NSData * data = UIImagePNGRepresentation(image);

然后在您的代码中执行此操作

[mc addAttachmentData:data mimeType:@"image/png" fileName:@"myscreenshot.png"];

答案 1 :(得分:0)

Hey Sendgrid Evangelist在这里,下面的代码使用iOS 7功能截取屏幕截图,我们的iOS sdk通过电子邮件发送图片。

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

- (IBAction)sendScreenshot:(id)sender {
    UIImage *screen = [self screenshot];
    sendgrid *msg = [sendgrid user:@"username" andPass:@"password"];

    msg.to = @"foo@bar.com";
    msg.subject = @"screenshot";
    msg.from = @"bar@foo.com";
    msg.text = @"screenshot";
    msg.html = @"<h1>screenshot</h1>";

    [msg attachImage:screen];
    [msg sendWithWeb];
}