2011-05-17 44 views
3

用戶拍攝照片後,我成功地將圖像保存到我的應用程序。我想要做的事情是,當用戶回到應用程序時,我希望他們能夠以附件的形式通過電子郵件發送照片。我沒有任何運氣將應用程序中的數據轉換爲圖像,所以我可以添加爲附件。請有人指點我正確的方向。這是拍攝照片後保存圖像的位置。從應用程序和電子郵件檢索已保存的圖像

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 


    //here is the image returned 

     app.aImage2 = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     NSData *imageData = UIImagePNGRepresentation(app.aImage2); 
     NSString * savedImageName = [NSString stringWithFormat:@"r%@aImage2.png",app.reportNumber]; 
     NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString * documentsDirectory = [paths objectAtIndex:0]; 
     NSString * dataFilePath; 
     dataFilePath = [documentsDirectory stringByAppendingPathComponent:savedImageName]; 
     [imageData writeToFile:dataFilePath atomically:YES]; 



    [self dismissModalViewControllerAnimated:YES]; 


} 

這裏是我需要附加它的地方。

//this is inside my method that creates an email composer view 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet 

    //how would i attach the saved image from above? 

回答

2

這包括代碼,邁克提到這裏:

How to add a UIImage in MailComposer Sheet of MFMailComposeViewController

此外,其他部分由薩加爾科塔裏的答案在這裏舉:

Sending out HTML email with IMG tag from an iPhone App using MFMailComposeViewController class

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    // Dismiss image picker modal. 
    [picker dismissModalViewControllerAnimated:YES]; 

    if ([MFMailComposeViewController canSendMail]) { 
     // Create a string with HTML formatting for the email body. 
     NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"]; 

     // Add some text to it. 
     [emailBody appendString:@"<p>Body text goes here.</p>"]; 

     // You could repeat here with more text or images, otherwise 
     // close the HTML formatting. 
     [emailBody appendString:@"</body></html>"]; 
     NSLog(@"%@", emailBody); 

     // Create the mail composer window. 
     MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init]; 
     emailDialog.mailComposeDelegate = self; 

     // Image to insert. 
     UIImage *emailImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 

     if (emailImage != nil) { 
      NSData *data = UIImagePNGRepresentation(emailImage); 
      [emailDialog addAttachmentData:data mimeType:@"image/png" fileName:@"filename_goes_here.png"]; 
     } 

     [emailDialog setSubject:@"Subject goes here."]; 
     [emailDialog setMessageBody:emailBody isHTML:YES]; 

     [self presentModalViewController:emailDialog animated:YES]; 
     [emailDialog release]; 
     [emailBody release]; 
    } 
} 
+0

有不有機會測試這些代碼,但這應該引導你在ri裏面ght方向。不太確定你的「app.aImage2」是什麼(我假設它是一個UIImage,你已經聲明在某處)。 – fuzz 2011-05-17 06:11:41

+0

對不起忘了添加「app.aImage2」是我的應用程序委託中的全局UIImage。這工作!我不得不編輯一點,但謝謝! – Louie 2011-05-17 15:57:21

+0

大部分代碼似乎都是基於Mike在這裏提供的:http://stackoverflow.com/questions/1527351/how-to-add-an-uiimage-in-mailcomposer-sheet-of-mfmailcomposeviewcontroller-in- iph/2461451#2461451,所以最好給出適當的引用。 – 2011-05-27 16:27:25

相關問題