當用戶在我的應用程序中點擊按鈕時,我想截取當前視圖並以該屏幕截圖圖像作爲附件打開文本消息。我如何在iOS7中執行此操作?以屏幕截圖並以編程方式發送
(我已經看到了如何採取截圖,但不服用的截圖並將其連接到一個消息,什麼職位)
謝謝!
當用戶在我的應用程序中點擊按鈕時,我想截取當前視圖並以該屏幕截圖圖像作爲附件打開文本消息。我如何在iOS7中執行此操作?以屏幕截圖並以編程方式發送
(我已經看到了如何採取截圖,但不服用的截圖並將其連接到一個消息,什麼職位)
謝謝!
對於採取截圖添加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.對於mail這個圖片連接,在構建階段添加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];
}
做一些修改:
發送圖像通過短信
// 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
{
}
採取的截圖,你可以使用Quartz Display Services
方法
你也可以傳遞RECT如
CGRect = [self bounds];
然後使用CGDisplayCreateImageForRect
CGImageRef selectedScreenImage;
selectedScreenImage = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect);
將其轉換爲NSData
NSData *data = (NSData *)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(selectedScreenImage)));
創建圖像
安d然後將其附加到使用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];
}
要發送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");
}
}
如果您需要獲取屏幕代碼,請告知我 –
感謝您的支持。有沒有可能做到這一點,而無需粘貼?只需在短信中自動添加圖片? – Kreutzer
http://stackoverflow.com/questions/19170516/mfmessagecomposeviewcontroller-ios7-addattachmentdatatypeidentifierfilename-n – Shubhank
採取截圖您的留言可以使用'石英顯示服務',即CGDisplayCreateImageForRect或CGDisplayCreateImage' –
是你的問題發送一個屏幕截圖作爲彩信?你有編碼捕捉屏幕截圖嗎? –