Skip to content

Instantly share code, notes, and snippets.

@benjaminhallock
Last active December 5, 2017 06:10
Show Gist options
  • Save benjaminhallock/46420606d8ef475d535d to your computer and use it in GitHub Desktop.
Save benjaminhallock/46420606d8ef475d535d to your computer and use it in GitHub Desktop.
How to Paste a GIF (keyboard).
/*
Make sure your using a GIF extension for UIImage... Most UIIMageViews will play it automatically w/ extensions.
I used this one ... https://github.com/mayoff/uiimage-from-animated-gif
This one is more effecient but more code too... https://github.com/Flipboard/FLAnimatedImage
*/
-(void)viewDidLoad
{
//Still trying to figure out why this is needed, can't paste the gif without it, but I get the double Paste action anywhere else.
[UIMenuController sharedMenuController].menuItems = @[[[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(customAction:)]];
}
-(void)customAction:(id)sender
{
// Depending on the keyboard, your going to have to check for this "data", somewhere, sometimes it might be a url, other times its hidden in these "data types", which is like a dictionary.
NSLog(@"%@ %@ %@", [UIPasteboard generalPasteboard].pasteboardTypes, [UIPasteboard generalPasteboard].URLs, [UIPasteboard generalPasteboard].images);
NSData *gifData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString *)[UIPasteboard generalPasteboard].pasteboardTypes.firstObject];
//Not neccessary but I'm just double checking.
if ([UIPasteboard generalPasteboard].image && gifData)
{
//Whatever your extension tells you to do.
UIImage *image = [UIImage animatedImageWithAnimatedGIFData:gifData];
// If you want you can test it
// UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// [self.view addSubview:imageView];
//Add it to your UITextView, but make sure you can retrieve it back out. Can't animate inside textView.
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = image;
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:textAttachment];
//For some reason, the font gets tinnier, so I have to save it before the paste.
UIFont *font = self.inputToolbar.contentView.textView.font;
self.inputToolbar.contentView.textView.attributedText = string;
self.inputToolbar.contentView.textView.font = font;
}
}
@rkittinger
Copy link

How do I 'retrieve image back out' and make it animate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment