2012-09-20 61 views
1

因此,将屏幕的一部分复制到剪贴板的代码很有用,因为它已成功将其应用于我的相册。但是,我希望能够将部分屏幕截图粘贴到新的SMS消息中。我知道它必须手动完成(长按消息和粘贴),但它没有粘贴任何东西,或没有粘贴选项(因为它将它保存为字符串)。代码的中间部分是我正在努力的部分。任何帮助都会很棒。我已将forPasteboardType更改为“图像”,但这也不起作用。将部分屏幕截图复制到粘贴板

//Capture part of Screen Shot 
     UIGraphicsBeginImageContext(self.view.bounds.size); 
     CGContextRef c = UIGraphicsGetCurrentContext(); 
     CGContextTranslateCTM(c, 0, 98); // 
     [self.view.layer renderInContext:c]; 
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

    //Send Screenshot to Pasteboard  
    UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:YES]; 
    pasteBoard.persistent = YES; 
    NSData *data = UIImagePNGRepresentation(viewImage); 
    [pasteBoard setData:data forPasteboardType:(NSString *)kUTTypePNG];  

    /////// Open SMS 
    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease]; 
    if([MFMessageComposeViewController canSendText]) 
    { 
     controller.body = @"Hello from me, paste image here -->"; 
     controller.recipients = [NSArray arrayWithObjects:@"123456789", nil]; 
     controller.messageComposeDelegate = self; 
     [self presentModalViewController:controller animated:YES]; 
    } 
    ////// End SMS 
} 
+0

所以,我去掉了中间部分,宣称 “UIPasteboard *纸板;”在我的.h文件中,添加了“pasteBoard = [UIPasteboard generalPasteboard];”在我的ViewDidLoad中,最后放上“pasteBoard.image = viewImage;”将图像发送到粘贴板。如果我手动打开一个新的SMS,我可以粘贴图像。我也在一封新邮件中工作。但是,当我尝试将其粘贴到我的应用程序中新创建的SMS中时,它不起作用?我想也许是因为我的身体有文字,所以我删除了“controller.body”这一行,但仍然不能。我觉得我很接近! – user1467534

+0

好的,我通过使用此代码来打开本机SMS应用程序,NSString * stringURL = @“sms:”; NSURL * url = [NSURL URLWithString:stringURL]; [[UIApplication sharedApplication] openURL:url]; – user1467534

+0

我现在可以将图像粘贴到本地SMS应用程序中。它仍然在我剪下的地方显示出一个白色边框。有关于此的任何想法? – user1467534

回答

1
//Capture part of Screen Shot 
UIGraphicsBeginImageContext(self.view.bounds.size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, 0, 98); // 
[self.view.layer renderInContext:c]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//Send Screenshot to Pasteboard  
UIPasteboard *pasteBoard = [UIPasteboard pasteboardWithName:UIPasteboardNameGeneral create:YES]; 
pasteBoard.persistent = YES; 
NSData *data = UIImagePNGRepresentation(viewImage); 
[pasteBoard setData:data forPasteboardType:(NSString *)kUTTypePNG];  

NSString *stringURL = @"sms:"; 
NSURL *url = [NSURL URLWithString:stringURL]; 
[[UIApplication sharedApplication] openURL:url]; 
+1

当我打开短信,发现图像不在里面。我相信这个形象不是零。 – RainCast