我有一个iPhone应用程序,可以从App内置的相册中选择照片。现在我想添加一个共享按钮,其中包含通过电子邮件分享此照片的选项,我可以通过此代码附加现有照片:
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];
但是,我需要在此代码中更改以将选定的相册附加到电子邮件正文中? 提前谢谢。
答案 0 :(得分:4)
使用UIImagePickerController
允许用户选择图片。然后它将调用此委托方法。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData* data = UIImageJPEGRepresentation(image, 1.0);
// Your e-mail code here
}
答案 1 :(得分:3)
嗨使用UIImagePicker从相机的PhotoLibrary中选择图像并使用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];
}
答案 2 :(得分:0)
假设您有一个来自图像选择器(或任何其他来源)的 UIImage ,您首先需要从图像中创建 NSData 对象。使用 UIImageJPEGRepresentation 或 UIImageJPEGRepresentation 功能。获得 NSData 对象后,将其添加为附件,就像您在发布的代码中所做的那样。
在大多数情况下,图像将显示在主邮件正文之后的电子邮件中。