Question

I'm trying to send a local image via the MFMailComposeViewController

I've decided to set a html body, so I can design the email sent and also display images in the body (to not have my images in attachment).

Images send are extracted from the current view. My code looks like :

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
NSMutableString *emailBody = [[NSMutableString alloc] init];

[emailBody appendString:@"<html><body>"];

///////CREATE IMAGE TO SHARE////////
UIGraphicsBeginImageContextWithOptions(viewToSend.frame.size,YES,0.0);
[viewToSend.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
[img drawAtPoint:CGPointZero];
UIImage *outputImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Convert image to a base64 string
NSData *imgData = UIImagePNGRepresentation(outputImg);
NSString *imageDataString = [imgData base64EncodedString];

[emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",imageDataString]];
[emailBody appendString:@"</body></html>"];

[mailer setMessageBody:emailBody isHTML:YES];

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

When the MFMailComposeViewController is displayed, the image appears in it. But in the received email, the image is missing.

My base 64 encoding method is taken from the class NSData + Base64 created by Matt Gallagher

What is happening?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top