0

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

3 回答 3

4

使用这个伴侣。它工作正常!

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

 }
于 2013-05-22T10:18:14.163 回答
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类参考

于 2013-05-22T10:15:36.880 回答
0

你可以简单地使用这条线来附加你的图像。

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

[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:@"image.png"];
于 2013-05-22T11:51:27.497 回答