如何使用MFMailComposerViewController将UIImage添加到电子邮件中?

时间:2013-05-22 10:13:06

标签: iphone ios objective-c uiimageview uiimage

我正在使用MFMailComposerViewController从iOS应用发送电子邮件。除尝试添加图像外,邮件仍可正常工作。我的图像问题是使用

我见过其他使用类似的例子:UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; 添加图像。我的图像是使用[self.photo objectForKey:kPhotoPictureKey];

从数据库表中获取的
     // mail
            // Email Subject
            NSString *emailTitle = @"Join. Download the iPhone app";
            // Email Content
            NSString *messageBody = @"http://www..com/";
            // To address
            NSArray *toRecipents = [NSArray arrayWithObject:@""];


            UIImageView *mailImage = [[UIImageView alloc] init];
            mailImage.image = [UIImage imageNamed:@"1.png"]; // placeholder image
            mailImage.file = [self.photo objectForKey:kPhotoPictureKey];
            [mailImage loadInBackground];

 NSString *messageBody = [NSString stringWithFormat:@"http://www.example.com/<p><b><img src='data:image/png;base64,%@'></b></p>",mailImage.image];


            MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
            mc.mailComposeDelegate = self;
            [mc setSubject:emailTitle];
            [mc setMessageBody:messageBody isHTML:NO];
            [mc setToRecipients:toRecipents];


            // Present mail view controller on screen
            [self presentViewController:mc animated:YES completion:NULL];

3 个答案:

答案 0 :(得分:3)

使用此配偶。它工作正常!!

if ([MFMailComposeViewController canSendMail]) {
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            [picker setSubject:@"SUBJECT OF THE MAIL!"];
            NSData *myData = UIImageJPEGRepresentation(IMAGE_TO_SEND, 0.9);
            [picker addAttachmentData:myData mimeType:@"image/jpg" fileName:@"IMAGE_NAME.jpg"];

            // Fill out the email body text
            NSString *emailBody = @"BODY OF THE MAIL";
            [picker setMessageBody:emailBody isHTML:NO];
            [self presentViewController:picker animated:YES completion:nil];

 }

答案 1 :(得分:0)

你必须使用

- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
Parameters
attachment
The data to attach. Typically, this is the contents of a file that you want to include. This parameter must not be nil.
mimeType
The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be nil.
filename
The preferred filename to associate with the data. This is the default name applied to the file when it is transferred to its destination. Any path separator (/) characters in the filename are converted to underscore (_) characters prior to transmission. This parameter must not be nil.
Discussion
This method attaches the specified data after the message body but before the user’s signature. You may attach multiple files (using different file names) but must do so prior to displaying the mail composition interface. Do not call this method after presenting the interface to the user.

Availability
Available in iOS 3.0 and later.

表格MFMailComposeViewController班级参考

答案 2 :(得分:0)

您只需使用此行附加图片即可。

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];

[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:@"image.png"];