当用户在我的应用程序中点击按钮时,我想要截取当前视图的屏幕截图,并打开一个带有该屏幕截图图像作为附件的短信。在iOS7中我该怎么做?
(我看到了如何截图的帖子,但没有关于如何截图并将其附加到消息的任何内容)
谢谢!
(我看到了如何截图的帖子,但没有关于如何截图并将其附加到消息的任何内容)
谢谢!
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
{
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result;
,在这里您可以使用[self dismissViewControllerAnimated:YES completion:nil];
。当取消/完成被点击或出现任何错误时,将调用此方法。 - iNoobUIWindow *window = [[UIApplication sharedApplication]keyWindow];UIView *viewForScreen = window.viewForBaselineLayout;
,但我不确定它是否有效。 - iNoob.m
中添加 MFMessageComposeViewControllerDelegate
并使用 messageComposer.delegate = self;
。之后实现代理方法,你将得到类型为 MessageComposeResult
的回调,它有三个选项 MessageComposeResultCancelled、MessageComposeResultSent 和 MessageComposeResultFailed
。看看这是否有帮助? - iNoobmessageComposer.messageComposeDelegate = self;
。 - KreutzerQuartz 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];
}
- (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");
}
}
Quartz Display Services
即CGDisplayCreateImageForRect
或CGDisplayCreateImage
。 - Himanshu Joshi