1

我正在通过我的应用程序发送电子邮件,我setMessageBody可以将正文设置为我的邮件,但我的要求是我想发送多个图像和文本 如图所示

使用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];
4

3 回答 3

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;
}

它将返回一个图像。您可以在邮件中附上这张图片。

于 2013-05-11T06:09:28.670 回答
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;

         }
于 2013-05-16T10:06:02.987 回答
1
[mailView addAttachmentData:emaildata mimeType:@"image/png" fileName:@"File"];

使用此行附加您想要添加的图像数量,并且

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

做那个isHTML=YES这样你就会得到正文中的所有图像而不是附件(嵌入正文中)

于 2013-05-13T05:33:40.390 回答