如何在iOS中将屏幕截图附加到mailcomposer

时间:2012-08-11 14:51:02

标签: iphone objective-c ios xcode mfmessagecomposeview

您好我正在使用以下代码将截屏附加到mailcomposer,我没有要检查的设备,所以这可以在实际设备上使用吗?

-(void)launchMailAppOnDevice
{
    /*Take a SnapShot of current screen*/
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

    NSString *recipients = @"mailto:xyz@abc.com?cc=@\"\"&subject=blah!!blah!!";

    NSString *body = @"&body=blah!!blah!!";

    NSString *email = [NSString stringWithFormat:@"%@%@%@", recipients, body,  imageData];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

2 个答案:

答案 0 :(得分:6)

最后5行是错误的。您最有可能想要使用MFMailComposeViewController类:

MFMailComposeViewController *mcv = [[MFMailComposeViewController alloc] init];

[mcv setSubject:@"blah!!blah!!"];
[mcv setToRecipients:[NSArray arrayWithObject:@"xyz@abc.com"]];
[mcv setMessageBody:@"Blah!! 'tis the body!" isHTML:NO];
[mcv addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"Screenshot.jpg"];

[someViewController presentModalViewController:mcv animated:YES];
[mcv release];

P上。 s。:不要忘记将MessageUI框架添加到项目中,还要#import <MessageUI/MessageUI.h>

P上。页。 s。:虽然在实际设备上进行测试很重要,但在编写实际代码之前阅读一些指南和文档更为重要。

答案 1 :(得分:1)

您需要添加:

NSData *imageData = UIImagePNGRepresentation(image);
[picker addAttachmentData:imageData mimeType:@"image/png"   fileName:@"fileName"];

如果你计划使用MFMailComposeViewController,你应该使用它而不是mailto:,但我不能强调总是在真实设备上测试你的应用程序是多么重要。