Re: NSSharingService with Animated GIFs?
Re: NSSharingService with Animated GIFs?
- Subject: Re: NSSharingService with Animated GIFs?
- From: Charles Carver <email@hidden>
- Date: Sat, 17 May 2014 01:41:55 -0400
Ken,
Thanks for the response. NSImage never ended up working, but your suggestion of attaching the local URL worked:
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];
I then implemented didShareItems and didFailToShareItems so that I could remove the file after sharing was complete:
- (void)sharingService:(NSSharingService *)sharingService didShareItems:(NSArray *)items{
NSString *path = items[0];
[self removeFile:path];
}
...
- (void)removeFile:(NSString *)path{
[[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
}
And for those struggling, I found the following method was required for everything to work properly:
- (NSWindow *)sharingService:(NSSharingService *)sharingService sourceWindowForShareItems:(NSArray *)items sharingContentScope:(NSSharingContentScope *)sharingContentScope{
return self.window;
}
I’m not sure if this is the best/most elegant way to accomplish this, so any further suggestions would be appreciated.
Thank you for your help once again,
Charles Carver
On May 16, 2014, at 9:51 PM, Ken Thomases wrote:
> On May 16, 2014, at 10:26 AM, Charles Carver wrote:
>
>>> I'm pretty new to Objective-C, but have been mostly understanding everything so far. I am stuck, however, on trying to share an animated GIF through NSSharingService.
>>>
>>> I am attaching the image like so, where “image" is a string containing the URL of an animated GIF (http://i.imgur.com/V8w9fKt.gif, for example):
>>>
>>> NSImage *imageData = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:image]];
>>> NSArray *shareItems = [NSArray arrayWithObjects:imageData, @“Text", nil];
>>> NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeMessage];
>>> service.delegate = self;
>>> [service performWithItems:shareItems];
>>>
>>> When the code is run and the message is sent, however, the image gets sent as a PNG file instead of a GIF.
>
> Instead of putting an NSImage into the list, try creating an NSPasteboardItem and setting its data to be the image data and its type to kUTTypeGIF:
>
> NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
> NSPasteboardItem* item = [[NSPasteboardItem alloc] init];
> [item setData:data forType:(__bridge NSString*)kUTTypeGIF];
> NSArray *shareItems = [NSArray arrayWithObjects:item, @“Text", nil];
> …
>
> It may still end up as a PNG because the pasteboard server will translate from any known image type to the most common interchange formats, and the receiving end gets to pick its preferred type.
>
> If that still doesn't work, you can try putting either the original URL into the shareItems array or the file URL from a locally-saved file.
>
> Regards,
> Ken
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden