2011-06-23 21 views

回答

12

您需要將兩個框架添加到您的項目中 - QuartzCoreMessageUI,然後執行#import <QuartzCore/QuartzCore.h>#import <MessageUI/MessageUI.h>

你按下按鈕的代碼應該是這樣的,

- (void)buttonPress:(id)sender 
{ 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData * imageData = UIImageJPEGRepresentation(image, 1.0); 

    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController * mailComposer = [[[MFMailComposeViewController alloc] init] autorelease]; 
     mailComposer.delegate = self; 
     [mailComposer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:@"attachment.jpg"]; 

     /* Configure other settings */ 

     [self presentModalViewController:mailComposer animated:YES]; 
    } 
} 
+0

迪帕克您好,我有什麼不清楚。 xcode如何知道我想要附加的截圖的路徑和名稱,因爲我沒有保存它? – Clarence

+0

好的。我在這裏做了一些假設。我假設你想要截取你應用的當前屏幕截圖。所以我做了'[self.view.layer renderInContext:..];''self'是你的視圖控制器。我將視圖繪製到圖像上,然後使用圖像上的「UIImageJPEGRepresentation」提取「NSData」對象以獲取JPEG表示。然後,我可以將'NSData'對象傳遞給方法'addAttachmentData:mimeType:fileName:',其中'fileName'是郵件的接收者將看到的那個。 –

+0

所以你不需要從文件系統中讀取來做到這一點。如果你有不同的要求,請告訴我。 –

相關問題