应用程序内截图并附加到电子邮件而不保存到库中

时间:2011-06-23 06:47:18

标签: xcode screenshot

我想知道我应该使用哪些代码,如果我想让我的应用程序能够通过按UI按钮截取屏幕截图并立即弹出并邮件撰写并通过电子邮件发送屏幕截图而不将其保存到照片库中?

非常感谢!

1 个答案:

答案 0 :(得分:12)

您需要在项目中添加两个框架 - QuartzCoreMessageUI,然后执行#import <QuartzCore/QuartzCore.h>#import <MessageUI/MessageUI.h>

你的按键代码应该是,

- (void)buttonPress:(id)sender
{
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

    if ( [MFMailComposeViewController canSendMail] ) {
        MFMailComposeViewController * mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
        mailComposer.delegate = self;
        [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"];

        /* Configure other settings */

        [self presentModalViewController:mailComposer animated:YES];
    }
}