如何在电子邮件正文中的某些文本后设置第一个图像?

时间:2012-04-09 05:11:34

标签: iphone objective-c cocoa-touch ios4 mfmailcomposeviewcontroller

我正在使用MFMailComposeViewController发送电子邮件,

使用此代码,我可以获取图像,但图像位于文本详细信息之后, 我不是第一张图片和文字之后,

这是我的代码,

   MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
    mail.mailComposeDelegate = self;

    if ([MFMailComposeViewController canSendMail]) {
        //Setting up the Subject, recipients, and message body.
        [mail setToRecipients:[NSArray arrayWithObjects:Emailid,nil]];
        [mail setSubject:@"Receipt"];

        NSData *photoData = UIImageJPEGRepresentation([UIImage imageNamed:@"Gift.png"], 1);
        [mail addAttachmentData:photoData mimeType:@"image/jpg" fileName:[NSString stringWithFormat:@"photo.png"]];

        [mail setMessageBody:@"Receipt" isHTML:NO];
        NSString *emailBody;




        emailBody = [NSString stringWithFormat:@
                     "<br>Purchase Amount:            </br> "      "$  %@"
                     "<br>Amount Received:        </br> "      "$  %@"
                     "<br>Change:    </br> "      "$  %@"
                     ,AmountData,txtAmount.text,ChangeAmount.text];  


        [mail setMessageBody:emailBody isHTML:YES];

请任何人建议我怎么办?

1 个答案:

答案 0 :(得分:6)

据我所知,这是mfmailcomposeviewcontroller的默认行为,而不是仅仅添加图像作为附件,你可以如下所示首先显示图像只是为了创建身体部位

(void)createEmail {


NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain];
   [emailBody appendString:@"<p>Some email body text can go here</p>"];
   UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"];
   NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];

   NSString *base64String = [imageData base64EncodedString];
  [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
  [emailBody appendString:@"</body></html>"];
    NSLog(@"%@",emailBody);

 //mail composer window
    MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
    emailDialog.mailComposeDelegate = self;
    [emailDialog setSubject:@"My Inline Image Document"];
    [emailDialog setMessageBody:emailBody isHTML:YES];

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

显示here