2014-04-14 18 views

回答

0

使用MFMailComposerController,並使用此代碼:

MFMailComposeViewController * mcvc = [[MFMailComposeViewController alloc] init]; 

mcvc = self; 

[mcvc setSubject:@"Subject"]; 

[mailer setToRecipients:@[@"[email protected]"]]; 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mrweb" ofType:@"png"]; 
NSData *imageData = [NSData dataWithContentsOfFile:filePath]; 
[mcvc addAttachmentData:imageData mimeType:@"image/png" fileName:@"allegato1.png"]; 

[mcvc setMessageBody:@"Hello bro !" isHTML:NO]; 

[self presentViewController:mcvc animated:YES completion:^{}]; 

如果你有一個UIImage使用

NSData *imageData = UIImagePNGRepresentation(myImage.image); 

轉換UIImageNSData

Here the complete list of all mime-types

3

使用MFMailComposeViewController

MFMailComposeViewController *郵件= [MFMailComposeViewController的alloc] INIT];

//確定文件名和擴展名

的NSString *延長= @ 「PNG」;

NSString * filename = @「email」;

//獲取資源路徑和讀使用NSData的

的NSString *文件路徑=文件[[一個NSBundle mainBundle] pathForResource:文件名ofType:擴展名];

NSData * fileData = [NSData dataWithContentsOfFile:filePath];

//確定MIME類型

NSString *mimeType; 
if ([extension isEqualToString:@"jpg"]) 
{ 
    mimeType = @"image/jpeg"; 
} 
else if ([extension isEqualToString:@"png"]) 
{ 
    mimeType = @"image/png"; 
} 
else if ([extension isEqualToString:@"doc"]) 
{ 
    mimeType = @"application/msword"; 
} 
else if ([extension isEqualToString:@"ppt"]) 
{ 
    mimeType = @"application/vnd.ms-powerpoint"; 
} 
else if ([extension isEqualToString:@"html"]) 
{ 
    mimeType = @"text/html"; 
} 
else if ([extension isEqualToString:@"pdf"]) 
{ 
    mimeType = @"application/pdf"; 
} 

//添加附件

[mail addAttachmentData:fileData mimeType:mimeType fileName:filename]; 
相關問題