从app到邮件的图像

时间:2012-10-24 11:25:13

标签: objective-c macos email

我怎样才能做“分享到邮件”之类的事情?就像在选择邮件时NSSharingServices一样。例如,我有NSImage,我希望在 image2 中获得结果。我该怎么做?有什么指针吗?

IMAGE1:
enter image description here

图像2:
enter image description here

要仅从文本创建消息,我可以这样做:

NSURL *     url;
url = [NSURL URLWithString:@"mailto:"
       "?subject="
       "&body=text"
       ];
(void) [[NSWorkspace sharedWorkspace] openURL:url];

但我不知道如何用图像创建消息。


我在使用ScriptingBridge框架时找到了添加附件的方法。代码:

MailApplication *mail = [SBApplication
                         applicationWithBundleIdentifier:@"com.apple.Mail"];

MailOutgoingMessage *emailMessage =
[[[mail classForScriptingClass:@"outgoing message"] alloc]
 initWithProperties:
 [NSDictionary dictionaryWithObjectsAndKeys:
  @"this is my subject", @"subject",
  @"this is my content", @"content",
  nil]];

[[mail outgoingMessages] addObject: emailMessage];

emailMessage.visible = YES;


NSString *attachmentFilePath = [NSString stringWithUTF8String:"<my provided file path>"];
if ( [attachmentFilePath length] > 0 ) {

    MailAttachment *theAttachment = [[[mail
                                       classForScriptingClass:@"attachment"] alloc]
                                     initWithProperties:
                                     [NSDictionary dictionaryWithObjectsAndKeys:
                                      attachmentFilePath, @"fileName",
                                      nil]];

    [[emailMessage.content attachments] addObject: theAttachment];
}
[emailMessage visible];

有效。但是如何将NSImage添加到附件? Maby我必须将NSImage写入临时文件,然后添加为附件并删除临时文件?或者是什么?或者maby我应该以某种方式将NSImage添加到身体?

1 个答案:

答案 0 :(得分:2)

像这样:

NSString *text = @"sometext";
NSImage *image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:@"/logo57.png"]];
NSArray * shareItems = [NSArray arrayWithObjects:text, image, nil];

NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
service.delegate = self;
[service performWithItems:shareItems];

还要确保将NSSharingServiceDelegate放在委托的头文件中。