-2

嗨,我正在使用下面的代码将屏幕截图附加到 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]];
}
4

2 回答 2

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. ps:虽然在实际设备上进行测试很重要,但在编写实际代码之前阅读一些指南和文档更为重要。

于 2012-08-11T15:03:09.123 回答
1

您需要添加:

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

如果您打算使用 a MFMailComposeViewController,您应该使用它而不是 mailto:,但我无法强调始终在真实设备上测试您的应用程序的重要性。

于 2012-08-11T14:55:48.780 回答