将照片附加到我的iPhone应用程序的电子邮件

我有一个iPhone应用程序,从应用程序内置的相册中挑选照片。 现在我想添加一个共享button,通过电子邮件分享这张照片,我可以附上一个现有的照片通过这个代码:

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@""]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@""]; [picker setToRecipients:toRecipients]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"]; // Fill out the email body text NSString *emailBody = @""; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker release]; 

但是,在这段代码中,我需要更改什么来将所选的相册附加到电子邮件正文中? 提前致谢。

使用UIImagePickerController允许用户select一个图像。 然后它会调用这个委托方法。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSData* data = UIImageJPEGRepresentation(image, 1.0); // Your e-mail code here } 

您好使用UIImagePicker从相机的PhotoLibrary中select图像和使用MFMailComposeViewController用于发送电子邮件。

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Dismiss PickerViewController [picker dismissModalViewControllerAnimated:NO]; // Get Image Fro Attachment UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; NSData* data = UIImageJPEGRepresentation(image, 1.0); // Setup Email Settings Like Subject, Message , Attachment MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; mailPicker.mailComposeDelegate = self; [mailPicker setSubject:@"Image Attachment Test"]; // Set recipients NSArray *toRecipients = [NSArray arrayWithObject:@"xyz.gmail.com"]; [mailPicker setToRecipients:toRecipients]; // Set body message here NSString *emailBody = @":)"; [picker setMessageBody:emailBody isHTML:NO]; // Attach Image as Data [mailPicker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"]; [self presentModalViewController:mailPicker animated:YES]; [mailPicker release]; } 

假设你有一个图像select器(或任何其他来源)的UIImage ,你首先需要从图像创build一个NSData对象。 使用UIImageJPEGRepresentationUIImageJPEGRepresentation函数。 一旦你有了NSData对象,就像你在你发布的代码中一样添加它作为附件。

在大多数情况下,图像将出现在邮件正文后面的电子邮件中。