简体   繁体   English

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

[英]Take screenshot and send it programmatically

When a user taps on a button in my app, I'd like to take a screenshot of the current view and open up a text message with that screenshot image as an attachment. 当用户点击我的应用程序中的按钮时,我想截取当前视图的屏幕截图并打开带有该屏幕截图图像作为附件的文本消息。 How can I do this in iOS7? 我怎么能在iOS7中这样做?

(I've seen posts on how to take a screenshot but not anything on taking a screenshot and attaching it to a message) (我已经看过关于如何拍摄截图的帖子,但没有关于拍摄截图并将其附加到消息上的任何内容)

Thanks! 谢谢!

1. For taking a screenshot add the QuartzCore framework, you can use UIGraphicsBeginImageContextWithOptions 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. For attaching this image in mail, add MessageUI framework in build phase. 2.要在邮件中附加此图像,请在构建阶段添加MessageUI框架。 And use this NSData for attaching, something like this 并使用此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];

    }

EDIT: 编辑:

3. Sending image through SMS 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];
  }

Handle the delegate callbacks from MFMessageComposeViewController 处理来自MFMessageComposeViewController的委托回调

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

}

To take the ScreenShot you can use Quartz Display Services methods 要使用ScreenShot,您可以使用Quartz Display Services方法

You can pass the rect as 您可以将矩形作为

CGRect = [self bounds];

Then create image using CGDisplayCreateImageForRect 然后使用CGDisplayCreateImageForRect创建图像

CGImageRef selectedScreenImage;
selectedScreenImage = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect);

Convert it to NSData 将其转换为NSData

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

And then Attach it to your message using MFMailComposeViewController 然后使用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];

    }

To send a MMS 发送彩信

- (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");
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM