2

I am not getting any idea about sharing an animated .gif images using UIActivityViewController.please help me to resolve this.thanks in advance

see this image .i am displaying this type of images and to share these images I am using uiactivity view but it is not animating .it's just displaying image 



if(imgFrameCount==1)

    {
        imgFrameCount++;
        NSURL *url1=[[NSBundle mainBundle]URLForResource:@"animated2" withExtension:@"gif"];
        self.firstImage=[UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url1]];
        self.frameImage.image = self.firstImage;
    }
else if (imgFrameCount==2)
{
    imgFrameCount++;
    NSURL *url2=[[NSBundle mainBundle]URLForResource:@"animated3" withExtension:@"gif"];
    self.firstImage=[UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url2]];
    self.frameImage.image = self.firstImage;
}
Suneha
  • 119
  • 1
  • 8

4 Answers4

7

FWIW, I was having the same problem when trying to include a UIImage in the ActivityItems array. It will not work w/ a generic UIImage, nor a UIImage using the UIImage+animagedGIF category, nor FLAnimatedImage.

To resolve this problem, at least on iOS8, simply add the gif to the ActivityItems array as an NSData object rather than UIImage.

For example, assuming the gif is being requested from a URL:

NSURL *imagePath = [NSURL URLWithString:@"http://i.imgur.com/X4oonnm.gif"];
NSData *animatedGif = [NSData dataWithContentsOfURL:imagePath];
NSArray *sharingItems = [NSArray arrayWithObjects: animatedGif, nil];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
Jordan Bonitatis
  • 1,527
  • 14
  • 12
  • 1
    where is photoData defined? – AlexKoren Jul 09 '15 at 20:34
  • @AlexKoren, sorry about that. That was a reference to an NSDictionary that contained extra information I wanted to include in the ActivityViewController (such as image title, caption, and url) I've updated the example to remove it since it is not necessary in providing a working solution – Jordan Bonitatis Jun 19 '16 at 22:55
2

This is a tricky problem because different share receivers handle the data differently. So the NSData solution mentioned in other answers does work when sharing to a couple different apps, but not for most of them. It seems that the receiver needs to be smart enough to decode the byte array and determine that it's indeed an animated gif, but most don't do that. A better plan seems to be to send a file reference with the proper extension since most apps will recognize a file extension. So my current strategy is taken from https://stackoverflow.com/a/13028336/158770 but writing to a file named "share.gif" or similar. When sharing this way almost all apps that can handle an animated gif, manage to receive it properly.

Community
  • 1
  • 1
Matt Hall
  • 2,569
  • 2
  • 23
  • 33
1

Try using this code.

NSArray *animeImages = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"tmp-0.png"],
                                    [UIImage imageNamed:@"tmp-1.png"],
                                    [UIImage imageNamed:@"tmp-2.png"],
                                    [UIImage imageNamed:@"tmp-3.png"],
                                    [UIImage imageNamed:@"tmp-4.png"],
                                    [UIImage imageNamed:@"tmp-5.png"],
                                    [UIImage imageNamed:@"tmp-6.png"],
                                    [UIImage imageNamed:@"tmp-7.png"],
                                    [UIImage imageNamed:@"tmp-8.png"],
                                    [UIImage imageNamed:@"tmp-9.png"],
                                    [UIImage imageNamed:@"tmp-10.png"],
                                    [UIImage imageNamed:@"tmp-11.png"],
                                    nil];
imageView.image = [UIImage animatedImageWithImages:animeImages duration:2.0];

You can now play around with this imageView and set its various properties like frame and hidden, etc. to make it work like a Activity Indicator. The images passed in the array are a breakdown of the animated gif. It can be easily done using any online tool like this one.

Akshay Bhalotia
  • 799
  • 4
  • 11
  • see this url...http://i.stack.imgur.com/YkGe1.gif thanks i have already done the coding to display images.actually i having .gif images... – Suneha Oct 29 '14 at 12:37
  • I am using direct .gif images to display and that are displaying fine but my problem is how we share those .gif images. – Suneha Oct 29 '14 at 12:40
  • What do you mean by **share**? And please post the code to directly display animated gifs in iOS. I've been searching that but did not find a potential solution. – Akshay Bhalotia Oct 29 '14 at 12:54
  • if(imgFrameCount==1) { imgFrameCount++; NSURL *url1=[[NSBundle mainBundle]URLForResource:@"animated2" withExtension:@"gif"]; self.firstImage=[UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url1]]; self.frameImage.image = self.firstImage; } see this my code and i am sorry for troubling you.. – Suneha Oct 29 '14 at 13:14
  • Yes, I did. Didn't run it though. Does it display animated gif in a UIImageView? – Akshay Bhalotia Oct 29 '14 at 13:24
  • it displayed.if we useNSURL *url1=[[NSBundle mainBundle]URLForResource:@"animated2" withExtension:@"gif"]; self.firstImage=[UIImage animatedImageWithAnimatedGIFURL:url];..i am getting animation – Suneha Oct 29 '14 at 13:26
  • i don't know how to share animated gif images please help me – Suneha Oct 31 '14 at 04:49
  • What do you mean by **share**, please specify – Akshay Bhalotia Oct 31 '14 at 06:03
  • Share means we will be sharing an images to our mail ,social networking(Facebook/twitter) using UIActivityViewController .same as in my i have to share this gif animated images.actually i am having many .gif images by selecting one particular image it should be shared..And inside this gif imageView i am having another image view . – Suneha Oct 31 '14 at 07:13
  • Oh. okay. Please don't dump your code here. Update it with proper fomatting in your question. And I haven't used UIActivityViewController but you may [read more about it here](http://nshipster.com/uiactivityviewcontroller/). – Akshay Bhalotia Oct 31 '14 at 07:24
  • kk.actually i know about UIActivityViewController but only thing is i don't know how to share url and uiimage by merging them.can you help me and thank you so much for your suggestion – Suneha Oct 31 '14 at 07:28
0

This should do it. Expanding on Jordan Bonitatis' answer. The trick is to create an NSData object with the contents of the NSURL.

- (void)share
{
  NSURL *imageURL = [NSURL URLWithString:@"http://...url..of..gif];
  NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
  NSArray *objectsToShare = @[imageData];

  UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

  NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                 UIActivityTypePrint,
                                 UIActivityTypePostToVimeo];

  activityVC.excludedActivityTypes = excludeActivities;

  [self presentViewController:activityVC animated:YES completion:nil];
}
JaredH
  • 2,338
  • 1
  • 30
  • 40