ios - 应用内截图并附加到电子邮件 IOS

标签 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];
}

最佳答案

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

关于ios - 应用内截图并附加到电子邮件 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20163037/

相关文章:

iOS 应用程序在 [ParseCrashReporting 启用] 上崩溃

objective-c - 如果将对象放到NSDictionary中,是否有指向它的强大指针?

c# - .Net SMTP 队列

Java邮件编码非英文字符

ios - 使用 iBeacon 时禁用 "Location Accuracy"消息

ios - Swift - 想要在单击 Collection View 单元格内的按钮时进行更改

iphone - 使用社交框架将图像添加到 Facebook

ios - 使用dispatch_time定期执行任务

ios - 您如何在 Storyboard 中获取 UISearchbar 的结果并在另一个视点中使用这些结果?

WordPress Hook 在发送之前修改电子邮件正文