我可以将我的应用程序中的图像复制到iOS中的邮件应用程序

时间:2014-04-14 09:58:52

标签: ios objective-c image copy paste

我正在开发本机iOS应用程序。

我有一个要求,就是将图像从我的应用程序复制到邮件应用程序,如何完成任务,请提示我。

先谢谢..

2 个答案:

答案 0 :(得分:3)

使用MFMailComposeViewController

MFMailComposeViewController * mail = [MFMailComposeViewController alloc] init];

//确定文件名和扩展名

NSString * extension = @“png”;

NSString * filename = @“email”;

//获取资源路径并使用NSData

读取文件

NSString * filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];

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

答案 1 :(得分:0)

使用MFMailComposerController并使用此代码:

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

mcvc = self;

[mcvc setSubject:@"Subject"];

[mailer setToRecipients:@[@"mail@domain.com"]];

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);

转换UIImage中的NSData

Here the complete list of all mime-types