UIImageView+animatedGIF always LOOPS
Asked Answered
M

3

6

I used class made by "mayoff" (Rob Mayoff) "UIImageView+animatedGIF" which was proposed in one of the answers here on stackoverflow. UIImageView+animatedGIF

With it I can import animated .gif images in UIImageView on iOS. This class works flawlessly, but the only problem is that .gif is always looping. No matter how I export it (I am exporting image from photoshop - 67 frames, set repeat to "once") it loops forever in UIImageView.

I am importing my .gif with these two lines:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"Loading" withExtension:@"gif"];
self.loadingImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];

very simple and it works. I have tried setting animationRepeatCount property to 1, as recomended by Rob but that didn't do the trick. Also I have tried setting the UIImageView's animationImages property to gifImage.images too, instead of just setting the view's image property to gifImage. In that case, .gif is not animating at all.

Any ideas how to play that .gif only once? I can think of many tricks (like setting last frame of the gif to show up after some time but I'd first try something simpler if possible).

Macrophage answered 25/7, 2013 at 17:1 Comment(0)
K
19

I took the test app from that project and changed the viewDidLoad method to this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];
    UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
    self.dataImageView.animationImages = testImage.images;
    self.dataImageView.animationDuration = testImage.duration;
    self.dataImageView.animationRepeatCount = 1;
    self.dataImageView.image = testImage.images.lastObject;
    [self.dataImageView startAnimating];
}

When the app launches, it animates the image once and then shows just the last frame. If I connect a button that sends [self.dataImageView startAnimating], then tapping the button runs the animation one more time, then stops again on the last frame.

Make sure you're setting your image view up the way I do above. Make sure you are not setting the image view's image property to the animated image. The image view's image property must be set to a non-animated image if you want the animation to stop.

Kery answered 28/7, 2013 at 4:52 Comment(5)
Rob, can you check out my question using UIImage+animatedGIF in a map view overlay #20559091. I can't get your category to animate the gif in the overlay.Interpretation
Yo, Rob! Thanks for this great category. I got the gif to animate and all that on a device, but I have a ton of linker errors when debugging in Xcode 6.3. The example project is targeting iOS 5...do you get a clean build without changing the architecture to 32bit in the current version of Xcode?Twoseater
@HimanshuGarg Sounds like you should post a new question, explaining what you tried, what happened, and what you wanted to happen.Kery
@robmayoff Really sorry. That was my mistake. Its working fine now. Actually i was also using and another Category and the methods are confused and calling their methods.Wally
repeatcount is the key.Noachian
H
1

I have forked "uiimage-from-animated-gif" made by "Dreddik" and added a delegate method

#pragma mark - AnimatedGifDelegate
- (void)animationWillRepeat:(AnimatedGif *)animatedGif
{
    NSLog(@"animationWillRepeat");
    //[animatedGif stop];
}

So you know the time when animation will repeat and do your own thing in middle of every repetition. You may count the number of times animation repeated and stop animation at a certain number reached.

The forked repository is at https://github.com/rishi420/Animated-GIF-iPhone

Husain answered 1/5, 2014 at 9:19 Comment(0)
G
0

Was having issues in start/stp animation & managing animation count So used this pod

imageViewObject.animate(withGIFNamed: "gif_name", loopCount: 1) {
            print("animating image")
    }

To start/stop animation :

imageViewObject.isAnimatingGIF ? imageViewObject.stopAnimatingGIF() : imageViewObject.startAnimatingGIF()    
Gorga answered 18/8, 2020 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.