如何将我的全局数组作为我的电子邮件作曲家的主体返回

时间:2013-08-05 12:58:34

标签: iphone nsarray mfmailcomposeviewcontroller

我在数组上有一些像AppDelegate的图像。在电子邮件编辑器中的imageArray我想把这个数组发送到我的示例代码

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:appDelegate.imageArray isHTML:NO];
[self presentViewController:composeViewController animated:YES completion:nil];

但是我收到的错误就像无法使用类型为NSMuttableArray的rvalue初始化NSString类型的参数

3 个答案:

答案 0 :(得分:4)

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:@"Images" isHTML:NO];
        for (int i = 0; i < [appDelegate.imageArray count]; i++)
        {
             UIImage *Image = [appDelegate.imageArray objectAtIndex:i];
             NSData *myData = UIImagePNGRepresentation(Image);
             [composeViewController addAttachmentData:myData mimeType:@"image/png"                  fileName:@"Image.png"];
        }

[self presentViewController:composeViewController animated:YES completion:nil];

答案 1 :(得分:0)

MessageBody是一个简单的字符串,如果要附加图像,可以使用以下内容:

[composeViewController addAttachmentData:imageData mimeType:@"image/jpeg" fileName:fileName];

您附加的文件将以不同的方式表示取决于类型。

答案 2 :(得分:0)

这里只需用你的Arr调用这个函数

-(NSMutableString)createImgMailBodyFromArray:(NSMutableArray *)arr{

    //Create a string with HTML formatting for the email body
    NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];


    for (int i = 0; i<[arr count]; i++) {
        UIImage *emailImage = [arr objectAtIndex:i];
        //Convert the image into data
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
        //Create a base64 string representation of the data using NSData+Base64
        NSString *base64String = [imageData base64EncodedString];
        //Add the encoded string to the emailBody string
        //Don't forget the "<b>" tags are required, the "<p>" tags are optional
        [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]];
    }


    return emailBody;

}

现在只需这样打电话

if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"example@email.com"]];
        [composeViewController setSubject:@"example subject"];
        [composeViewController setMessageBody:[self createImgMailBodyFromArray:YOUR_ARR] isHTML:NO];
[self presentViewController:composeViewController animated:YES completion:nil];
    }

此处有“NSData + base64”文件,您可以找到https://github.com/l4u/NSData-Base64