在Yosemite在自己的应用程序中使用图像编辑扩展 [英] Using Image Editing extensions on Yosemite in own app
问题描述
在我的OS X应用程序,我想让用户编辑图像与相应的操作扩展安装在他/她的Mac上,例如图像标记扩展如目前在Mail.app或TextEdit(对于RTFD文件的图像)或Pixelmator的修复工具(如果可用)。就我可以回想起来,苹果公司宣布在WWDC'14将有一个公共API的这个任务。
不幸的是,我找不到任何起点如何使用扩展从主机应用程序的角度来看,既不是文档也不是示例代码。
我发现你必须将NSSharingPicker的未记录的style属性设置为non-如下所示:
- (IBAction)testSharingPicker:(id)sender
{
NSSharingServicePicker * picker = [[NSSharingServicePicker alloc] initWithItems:@ [[self.listing.images.firstObject thumbImage]]];
[picker setValue:@(1)forKey:@style];
[picker setDelegate:self];
[picker showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
}
设置样式值后,因为 - (NSArray *)sharedServicePicker:(NSSharingServicePicker *)sharedServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices
您还需要实现一个未记录的委托方法:
- (BOOL)sharedServicePicker:(NSSharingServicePicker *)sharedService shouldShowForView:(NSView *)inView
{
return YES;
}
但是,选择器不显示。我得到的是在发件人
按钮周围的一些奇怪的边框。不幸的是,苹果似乎只通过 NSTextView 来暴露动作扩展。
)
你提到的奇怪的边框似乎是当在
NSTextView
中的图片上悬停时显示的共享服务选择器,但是被
我偶然发现了您的GitHub repo ,我在此拉取请求中了解到的内容。
我已经通过提出
NSSharingServicePicker
按照你的问题描述的方式实现了一个hacky解决方法,然后持有NSSharingService在
。然后可以使用sharingServicePicker:sharingServicesForItems:proposedSharingServices:
中返回performWithItems:
调用服务本身,并在sharingService:didShareItems:
中返回数据
我的代码此处。
In my OS X app, I want to let the user edit images with the appropriate action extensions installed on his/her Mac, e.g the image markup extension as present in Mail.app or TextEdit (for RTFD files with images) - or Pixelmator's repair tool if available. As far as I can recall, Apple announces at WWDC '14 there would be a public API for this task.
Unfortunately I cannot find any starting point on how to use extensions from a host app perspective, neither documentation- nor sample-code-wise.
I found out that you have to set the undocumented style property of the NSSharingPicker to a non-zero value like this:
- (IBAction)testSharingPicker:(id)sender { NSSharingServicePicker *picker = [[NSSharingServicePicker alloc] initWithItems:@[[self.listing.images.firstObject thumbImage]]]; [picker setValue:@(1) forKey:@"style"]; [picker setDelegate:self]; [picker showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge]; }
Once the style value is set, you know you are on the right track, because
- (NSArray *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NSArray *)proposedServices
is called with the image editing extensions installed on my system, instead of the regular sharing extensions.You also need to implement an undocumented delegate method:
- (BOOL)sharingServicePicker:(NSSharingServicePicker *)sharingService shouldShowForView:(NSView*) inView { return YES; }
But still, the picker is not showing up. All I get is some weird border around the
sender
button.解决方案Unfortunately, it seems Apple only really exposes Action Extensions through
NSTextView
at the present time.On OS X, NSTextView plays the central role in presenting Extensions to the users.
That is achievable by creating a
NSTextView
and inserting an image into it as aNSFileWrapper
, for example (this code adapted from TextEdit):NSMutableAttributedString *attachments = [[NSMutableAttributedString alloc] init]; NSError *error; NSFileWrapper *wrapper = [[NSFileWrapper alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/user/Downloads/Test.png"] options:NSFileWrapperReadingImmediate error:&error]; if (wrapper) { NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper]; [attachments appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]]; } if ([attachments length] > 0) { NSRange selectionRange = [self.textView selectedRange]; if ([self.textView shouldChangeTextInRange:selectionRange replacementString:[attachments string]]) { [[self.textView textStorage] replaceCharactersInRange:selectionRange withAttributedString:attachments]; [self.textView didChangeText]; } }
(Note that inserting an image as a
NSTextAttachmentCell
causes a weird crash – rdar://20333977)That weird border you mention seems to be the Sharing Service Picker that's shown when hovering over an image in a
NSTextView
, however obscured slightly by the button.I stumbled on your GitHub repo and have contributed what I've learnt about it in this pull request.
I've achieved a hacky workaround by presenting the
NSSharingServicePicker
as described in your question and then holding onto theNSSharingService
s returned insharingServicePicker:sharingServicesForItems:proposedSharingServices:
. The services themselves can then be invoked withperformWithItems:
and data is returned insharingService:didShareItems:
.My code is available here.
这篇关于在Yosemite在自己的应用程序中使用图像编辑扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!