简体   繁体   中英

How to share an gif animated images in iOS

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;
}

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];

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.

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 .

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM