我想知道截取屏幕截图并调出电子邮件窗口并将屏幕截图附加到电子邮件的代码,只需在应用程序中按一个按钮即可。到目前为止,我已经编写了通过按下按钮调出电子邮件窗口的代码,我希望使用相同的按钮获取并附上屏幕截图。如果您能指出正确的方向,我将不胜感激。
- (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/