1

我有一个 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];

但是我需要在此代码中进行哪些更改才能将选择的相册附加到电子邮件正文中?提前致谢。

4

3 回答 3

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
}
于 2012-10-19T03:54:09.280 回答
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];


}
于 2012-10-19T05:37:27.640 回答
0

假设你有一个来自图像选择器(或任何其他来源)的UIImage,你首先需要从图像创建一个NSData对象。使用UIImageJPEGRepresentationUIImageJPEGRepresentation函数。获得NSData对象后,将其添加为附件,就像您在发布的代码中所做的那样。

在大多数情况下,图像会出现在邮件正文之后。

于 2012-10-19T03:58:00.260 回答