添加图像作为电子邮件的附件

时间:2012-02-07 10:20:23

标签: iphone objective-c

  

可能重复:
  how can send a file as attachment in objective c

我想在我的iPhone应用程序中添加一个图像作为附件。现在我附加了这样的图像:

NSMutableString * body = [[NSMutableString alloc] initWithString:@"<html><body><img src=\"http://url here\"/>"];
[body appendString:@"</body></html>"];
[mailer setMessageBody:body isHTML:YES];

但是我想要在我的资源文件夹中包含一个图像,而不是网址。

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

关于MFMailComposeViewController的Apple文档;基本上你必须用你的图像初始化UIImage,提取图像数据(例如:如果它是PNG使用UIImagePNGRepresentation())并提供正确的mime类型,例如图像/ PNG。然后使用下面的指定函数(文件名是您要为附件提供的文件的名称;例如“image.png”)

addAttachmentData:mimeType:fileName:
Adds the specified data as an attachment to the message.

- (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.

示例:


UIImage *myImage = [UIImage imageNamed:@"my_bundled_image"];
NSData *myImageData = UIImagePNGRepresentation(myImage);
[mailComposerViewController addAttachmentData:myImageData mimeType:@"image/png" fileName:"my_image.png"];