我对目标C很陌生,但到目前为止我对一切都很了解。然而,我被困在试图通过NSSharingService分享一个动画GIF上。
我是这样附加图像的,其中image是一个包含动画GIF (例如http://i.imgur.com/V8w9fKt.gif)的URL的字符串:
NSImage *imageData = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:image]];
NSArray *shareItems = [NSArray arrayWithObjects:imageData, href, nil];
NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage];
service.delegate = self;
[service performWithItems:shareItems];但是,当代码运行并发送消息时,图像将作为PNG文件而不是GIF发送。
我怀疑图像要么被NSImage或NSData夷为平地,而且我需要先将映像保存到磁盘,然后尝试发送。不过,我想知道,如果不采取额外的节省措施,是否能做到这一点。
编辑1
我发现了一个试图回答类似问题的GitHub回购。然而,一直没有找到解决办法,但最后一点建议是:
但是,当我将带有GIF附件的
NSAttributedString添加到NSSharingServicePicker时,共享图像并不是动画的。我不能将包装器RTFD数据添加到选择器中,因为它只能共享支持NSPasteboardWriting协议的对象,并且RTFD作为NSData返回。 在RTFD工作和保存动画时将NSRTFDPboardType数据复制到剪贴板上
是否可以将GIF转换为RTDF对象,将其复制到pasteboard,检索pasteboard项,然后共享该对象?还是不可能用NSSharingService保存动画?
编辑2
正如@Cocoadelica在评论中提到的那样,我想知道是否需要CoreImage来保存动画。我试图先将GIF文件保存到硬盘驱动器,然后将其加载到NSImage中,但它再次将其转换为静态PNG。
这是非常非常令人沮丧的。
发布于 2014-05-18 00:32:22
最后,我通过Cocoa邮件列表得到了回复。基本上,您需要附加一个直接链接到文件的NSURL。它不适用于外部图像,而且从未使用过NSImage:
NSString *fileUrl = @"http://i.imgur.com/V8w9fKt.gif";
NSString *fileName = [fileUrl lastPathComponent];
NSURL *saveUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@", NSTemporaryDirectory()]];
saveUrl = [saveUrl URLByAppendingPathComponent:fileName];
// Write image to temporary directory
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileUrl]];
[data writeToURL:saveUrl atomically:YES];
// Attach the raw NSURL pointing to the local file
NSArray *shareItems = [NSArray arrayWithObjects:saveUrl, @"Text", nil];
// Open share prompt
NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage];
service.delegate = self;
[service performWithItems:shareItems];然后,我实现了didShareItems和didFailToShareItems,以便在共享完成后可以删除文件:
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items{
NSString *path = items[0];
[self removeFile:path];
}
...
- (void)removeFile:(NSString *)path{
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
}对于那些苦苦挣扎的人,我发现每件事都需要以下方法才能正常工作:
- (NSWindow *)sharingService:(NSSharingService *)sharingService sourceWindowForShareItems:(NSArray *)items sharingContentScope:(NSSharingContentScope *)sharingContentScope{
return self.window;
}我意识到其中的一些代码是不正确的(我的URLWithString创建是违反直觉的,但我正在学习),但是这应该会让那些挣扎的人成为一个起点。
https://stackoverflow.com/questions/23598158
复制相似问题