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

时间:2014-03-13 07:10:02

标签: ios sms screenshot mms

当用户点按我的应用中的按钮时,我想要截取当前视图的屏幕截图并打开带有该屏幕截图图像作为附件的文字消息。我怎么能在iOS7中做到这一点?

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

谢谢!

3 个答案:

答案 0 :(得分:2)

1。要获取屏幕截图,请添加QuartzCore框架,您可以使用UIGraphicsBeginImageContextWithOptions

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
{

}

答案 1 :(得分:0)

要使用ScreenShot,您可以使用Quartz Display Services方法

您可以将矩形作为

传递
CGRect = [self bounds];

然后使用CGDisplayCreateImageForRect

创建图片
CGImageRef selectedScreenImage;
selectedScreenImage = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect);

将其转换为NSData

NSData *data = (NSData *)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(selectedScreenImage)));

然后使用MFMailComposeViewController

将其附加到您的邮件中
if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *cvc = [[MFMailComposeViewController alloc] init];
        cvc.mailComposeDelegate = self;

       [cvc addAttachmentData:data mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"YOUR_IMAGE.png"]];  

        [self presentModalViewController:cvc animated:YES];

    }

答案 2 :(得分:0)

发送彩信

- (UIImage *) imageFromViewIniOS7
{
UIImage* image = nil;

UIGraphicsBeginImageContext(contentScrollview.contentSize);
{
    CGPoint savedContentOffset = contentScrollview.contentOffset;
    CGRect savedFrame = contentScrollview.frame;

    contentScrollview.contentOffset = CGPointZero;
    contentScrollview.frame = CGRectMake(0, 0, contentScrollview.contentSize.width, contentScrollview.contentSize.height);
    if ([[NSString versionofiOS] intValue]>=7)
    {
        [contentScrollview drawViewHierarchyInRect:contentScrollview.bounds afterScreenUpdates:YES];

    }
    else
    {
        [contentScrollview.layer renderInContext: UIGraphicsGetCurrentContext()];

    }
    image = UIGraphicsGetImageFromCurrentImageContext();

    contentScrollview.contentOffset = savedContentOffset;
    contentScrollview.frame = savedFrame;
}
UIGraphicsEndImageContext();


return image;
}
-(void)buttonAction
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
pasteboard.persistent = YES;
pasteboard.image = [self imageFromViewIniOS7];

NSString *phoneToCall = @"sms:";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];    

if([MFMessageComposeViewController canSendText]) {
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"Your Email Body"];
picker.messageComposeDelegate = self;
picker.recipients = [NSArray arrayWithObject:@"123456789"];
[picker setBody:emailBody];// your recipient number or self for testing
picker.body = emailBody;
NSLog(@"Picker -- %@",picker.body);
[self presentModalViewController:picker animated:YES];
NSLog(@"SMS fired");
}
}