【问题标题】:Image from app to mail从应用程序到邮件的图像
【发布时间】:2013-01-18 09:21:57
【问题描述】:

如何执行“共享到邮件”之类的操作?在选择邮件时就像在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 添加到正文?

【问题讨论】:

    标签: objective-c macos email


    【解决方案1】:

    像这样:

    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-06-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多