ios – 截取屏幕截图并以编程方式发送

当用户点击我的应用程序中的按钮时,我想截取当前视图的屏幕截图并打开带有该屏幕截图图像作为附件的文本消息.我怎么能在iOS7中这样做?

(我已经看过关于如何拍摄截图的帖子,但没有关于拍摄截图并将其附加到消息上的任何内容)

谢谢!

最佳答案 1.为了拍摄截图添加QuartzCore框架,可以使用UIGraphicsBegin
ImageContextWithOptions

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too

2.要在邮件中附加此图像,请在构建阶段添加MessageUI框架.并使用此NSData进行附加,如下所示

//Check if mail can be sent
if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;

       // Add NSData you got as screenshot to attachment
       [mailer addAttachmentData:theImageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"test.jpg"]];  
        [self presentModalViewController:mailer animated:YES];

    }

编辑:

3.通过短信发送图像

    // Will Work only for iOS 7

    MFMessageComposeViewController* messageComposer = [[MFMessageComposeViewController alloc] init];
     messageComposer.messageComposeDelegate = self; // As mentioned by the OP in comments, we have to set messageComposeDelegate to self.
     messageComposer.recipients = [NSArray arrayWithObject:@"123456789"];

 if([MFMessageComposeViewController canSendText])
  {

    if([MFMessageComposeViewController respondsToSelector:@selector(canSendAttachments)] && [MFMessageComposeViewController canSendAttachments])
    {
        NSString* uti = (NSString*)kUTTypeMessage;
        [messageComposer addAttachmentData:theImageData typeIdentifier:uti filename:@"filename.jpg"];
    }

    [self presentViewController:messageComposer animated:YES completion:nil];
  }

处理来自MFMessageComposeViewController的委托回调

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{

}
点赞