2012-02-07 114 views
-1

可能重複:
how can send a file as attachment in objective c添加圖片作爲附件的電子郵件

我想圖像添加爲我的iPhone application.Now附件我附着圖像這樣:

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

但是,而不是URL我想包括一個圖像,這是在我的資源文件夾。

我該怎麼辦?

+1

重複:HTTP: //stackoverflow.com/questions/3049259/iphone-sdk-add-image-to-the-body-of-an-email,http://stackoverflow.com/questions/819021/iphone-how-can-i-嵌入圖像在電子郵件從我的應用程序和http://stackoverflow.com/questions/2534217/display-local-image-in-iphone-html-mail – Raptor 2012-02-07 10:23:39

回答

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