2014-02-13 30 views
0

我對Objective-C非常陌生。我試圖從屏幕截圖中創建一個圖像並將其發送到消息界面以作爲消息發送。我看了很多Stackoverflow的問題,並建議使用snapshotViewAfterScreenUpdates這是iOS 7中的新功能。它返回一個UIView指針,但我不知道如何從中創建圖像,然後再將消息接口作爲消息發送。任何幫助將不勝感激。如何從截圖創建圖像併發送到消息接口

+0

谷歌'UIGraphicsBeginImageContext' –

回答

2
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();  
NSData *newPNG=UIImagePNGRepresentation(img); // or you can use JPG or PDF 

要共享的圖像,那麼你可以使用UIActivityViewController

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObjects:@"I would like to share it.",newPNG, nil] applicationActivities:nil]; 
activityVC.excludedActivityTypes = @[ UIActivityTypeMessage ,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll]; 
[self presentViewController:activityVC animated:YES completion:nil]; 
+0

這個工作。非常感謝幫忙。 –

2

這是你,你從屏幕上獲得的UIImage:

UIGraphicsBeginImageContext(self.bounds.size); 
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
相關問題