2011-12-29 31 views

回答

6

你好我有工作使用下面的代码,它的工作在iphone和ipad中完美。我有使用这段代码的图像文件。其工作代码。

- (void)createEmail { 
//Create a string with HTML formatting for the email body 
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain]; 
//Add some text to it however you want 
[emailBody appendString:@"<p>Some email body text can go here</p>"]; 
//Pick an image to insert 
//This example would come from the main bundle, but your source can be elsewhere 
UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; 
//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]]; 
//You could repeat here with more text or images, otherwise 
//close the HTML formatting 
[emailBody appendString:@"</body></html>"]; 
NSLog(@"%@",emailBody); 

//Create the mail composer window 
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init]; 
emailDialog.mailComposeDelegate = self; 
[emailDialog setSubject:@"My Inline Image Document"]; 
[emailDialog setMessageBody:emailBody isHTML:YES]; 

[self presentModalViewController:emailDialog animated:YES]; 
[emailDialog release]; 
[emailBody release]; 
} 
+1

@parag嗨,你的代码,不工作,当我检查通过发送给收件人,它是作为一个破碎的消息发送的。 – Ranjit 2012-08-30 12:30:36

2

你好,我有工作在iPhone上使用下面的代码完美地工作和iPad。我有使用此代码attacha图像文件。其工作代码。

和iOS 3.0或更高版本直接插在iPhone连接图像

UIImage * image = [UIImage imageWithContentsOfFile:imagePath]; 
[composer addAttachmentData:UIImageJPEGRepresentation(itemImage, 1) mimeType:@"image/jpeg" fileName:@"MyFile.jpeg"]; 
+0

这正是OP要求的。当然,如果您想要以PNG(或其他类型的图像格式)发送,那么您需要使用适当的UIImageXXXRepresentation函数和image/XXX MIME类型。 – hds 2012-08-27 10:38:20

0

//追加附件:

//Create a string with HTML formatting for the email body 
NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"] ; 
//Add some text to it however you want 
[emailBody appendString:@"<p>Some email body text can go here</p>"]; 
//Pick an image to insert 
//This example would come from the main bundle, but your source can be elsewhere 
NSString *filePath = @"";/*you file path*/ 
//Convert the file into data 
NSData *attachmentData = [NSData dataWithContentsOfFile:filePath]; 
//Create a base64 string representation of the data using NSData+Base64 
NSString *base64String = [attachmentData 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]]; 
//You could repeat here with more text or images, otherwise 
//close the HTML formatting 
[emailBody appendString:@"</body></html>"]; 
NSLog(@"%@",emailBody); 

//Create the mail composer window 
MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init]; 
email.mailComposeDelegate = self; 
[email setSubject:@"My Inline Document"]; 
[email setMessageBody:emailBody isHTML:YES]; 

[self presentModalViewController:email animated:YES]; 
[email release]; 
[emailBody release]; 
+0

文件路径可以是任何文档目录路径或NSBundle路径。该文件可能是图像/ pdf /附件。要执行上面的代码,需要添加base64类进行编码。 – RajKrish 2013-07-12 13:10:20

相关问题