我正在开发本机iOS应用程序。
我有一个要求,就是将图像从我的应用程序复制到邮件应用程序,如何完成任务,请提示我。
先谢谢..
答案 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
。