我正在尝试通过MFMailComposeViewController
我决定设置一个html正文,这样我就可以设计发送的电子邮件,并在正文中显示图像(不附带我的图像)。
从当前视图中提取图像发送。我的代码如下:
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];
显示MFMailComposeViewController
时,图像会显示在其中。但在收到的电子邮件中,图片丢失了。
我的base 64编码方法取自NSData + Base64
Matt Gallagher
发生了什么事?