1

如何执行“共享到邮件”之类的操作?就像在NSSharingServices选择邮件时一样。例如,我有NSImage并且我想达到image2中的结果。我该怎么做?任何指针?

图片1:
在此处输入图像描述

图片2:
在此处输入图像描述

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

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 添加到附件中?也许我必须将 NSImage 写入临时文件,然后添加为附件并删除临时文件?或者是什么?或者我应该以某种方式将 NSImage 添加到正文中?

4

1 回答 1

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 放在委托的头文件中。

于 2013-01-18T01:21:57.977 回答