将多个文本和图像发送到电子邮件正文

时间:2013-05-11 05:59:21

标签: iphone ios objective-c mfmailcomposeviewcontroller

我正在使用setMessageBody通过我的应用发送电子邮件我可以将正文设置为我的邮件,但我的要求是我要发送多个图片和文字as show in image

使用addAttachmentData两次,我可以发送两张图片,但我想发送文字图片文字图片。

使用html在gmail中不起作用 有可能这样发送吗?

MFMailComposeViewController *mailView= [[MFMailComposeViewController alloc] init];
            mailView.mailComposeDelegate=self;
            [mailView setSubject:@"ABCD"];
              [mailView setMessageBody:@"Hai" isHTML:NO];
            UIImage * emailimage = [UIImage imageNamed:@"Iconpaid.png"];
            NSData * emaildata = UIImageJPEGRepresentation(emailimage, 1.0);

          [mailView addAttachmentData:emaildata mimeType:@"image/png" fileName:@"File"];

           // Here i want to add text

          [mailView addAttachmentData:emaildata mimeType:@"image/png" fileName:@"File"];
            [mailView setToRecipients:[NSArray arrayWithObject:@""]]; //[NSArray arrayWithObject:appDelegate.mechanicEmail]];
            [self presentModalViewController:mailView animated:YES];

3 个答案:

答案 0 :(得分:2)

您可以获取视图的屏幕截图。使用以下代码。

UIView *totalContentView = [[UIView alloc] init];
[totalContentView setFrame:CGRectMake(0, 0, 320, 460)];
UIImageView *image1 = [[UIImageView alloc] init];
[image1 setFrame:CGRectMake(0, 0, 320, 200)];
[image1 setImage:[UIImage imageNamed:@"image1.png"]];
UILabel *textLabel = [[UILabel alloc] init];
[textLabel setBackgroundColor:[UIColor clearColor]];
[textLabel setFrame:CGRectMake(0, 200, 320, 50)];
[textLabel setText:@"Its working fine"];
UIImageView *image2 = [[UIImageView alloc] init];
[image2 setFrame:CGRectMake(0, 250, 320, 200)];
[image2 setImage:[UIImage imageNamed:@"image2.png"]];
[totalContentView addSubview:image1];
[totalContentView addSubview:textLabel];
[totalContentView addSubview:image2];
[self getAsImageForView:totalContentView forRect:CGRectMake(0, 0, 320, 460)];

并遵循getAsImageForView方法,

-(UIImage *)getAsImageForView:(UIView *)view forRect:(CGRect)rect;
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef tmp = CGImageCreateWithImageInRect(image.CGImage, rect);//CGRectMake(240, 0, 240, 320)
    UIImage *cuttedImage = [UIImage imageWithCGImage:tmp];
    CGImageRelease(tmp);
    return cuttedImage;
}

它将返回一张图片。您可以在邮件中附加此图片。

答案 1 :(得分:2)

我有同样的问题,我找到了答案。见以下链接

MFMail

您可以轻松设置文字和图片,文字和图片。

使用此功能设置邮件正文

     - (NSString *)messageBody
     {
         // if we couldn't fetch the app information, use a simple fallback template
         if (self.applicationSellerName==nil) {
             // Fill out the email body text
             NSMutableString *emailBody = [NSMutableString stringWithFormat:@"<div> \n"
                                  "<p style=\"font:17px Helvetica,Arial,sans-serif\">%@</p> \n"
                                  "<h1 style=\"font:bold 16px Helvetica,Arial,sans-serif\"><a target=\"_blank\" href=\"%@\">%@</a></h1> \n"
                                  "<br> \n"
                                  "<table align=\"center\"> \n"
                                  "<tbody> \n"
                                  "<tr> \n"
                                  "<td valign=\"top\" align=\"center\"> \n"
                                  "<span style=\"font-family:Helvetica,Arial;font-size:11px;color:#696969;font-weight:bold\"> \n"
                                  "</td> \n"
                                  "</tr> \n"
                                  "<tr> \n"
                                  "<td align=\"left\"> \n"
                                  "<span style=\"font-family:Helvetica,Arial;font-size:11px;color:#696969\"> \n"
                                  "Please note that you have not been added to any email lists. \n"
                                  "</span> \n"
                                  "</td> \n"
                                  "</tr> \n"
                                  "</tbody> \n"
                                  "</table> \n"
                                  "</div>",
                                  self.message,
                                  [self.appStoreURL absoluteString],
                                  self.applicationName
                                  ];

             return emailBody;

         }

答案 2 :(得分:1)

[mailView addAttachmentData:emaildata mimeType:@"image/png" fileName:@"File"];

使用此行可以附加您想要添加的图像数量和

[mailView setMessageBody:@"Hai" isHTML:YES];

使那个isHTML = YES这样你就可以得到身体中的所有图像而不是作为附件(在嵌入体中)