Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated Image to UIImageView

I have to set an animated Image to UIImageView. see the following example of Image which I want to set on my UIImageView which has extension .GIF and I converted it into .PNG formate.

enter image description here

I already tried too many way to do it. but not get expected one. I need this technique to create my own custom Progressbar. I assign this image to my UIImageView. but this is not animating.. If any one have any idea then please share it with me.

like image 389
Sarat Patel Avatar asked Apr 25 '26 10:04

Sarat Patel


2 Answers

You can't use .gif images in iOS devices. GIF animations not supported.

Instead of that you can add each frame of that .gif as a .png or .jpg image and you can animate it by code.

NSArray *animateImagesArray = [NSArray arrayWithObjects:myUIImageOne, myUIImageTwo, myUIImageThree, nil];
yourImageView.animationImages      = animateImagesArray;
yourImageView.animationDuration    = 1.0f;
yourImageView.animationRepeatCount = 0; // Repeat forever
[yourImageView startAnimating];

Another option is, you can put a UIWebView and load your gif in it.

like image 163
Midhun MP Avatar answered Apr 28 '26 01:04

Midhun MP


For use file with.gif extension you can use this third-part library FLAnimatedImage. It is nice library with demo app.

You can also change some code to make work it as you want. If you have array of image (.png or another extension) you can use APPLE UIImageView:

NSMutableArray* animation = [[NSMutableArray alloc] init];
    for(int i = 0; i<20; i++)
    {
        [animation addObject:[UIImage imageNamed:[NSString stringWithFormat:@"image_%i",i]]];
    }

    UIImageView *player = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [player setAnimationImages:animation];
    [player setAnimationRepeatCount:0]; //0 means infinite loop
    player.animationDuration = 2.f;// time for one hole loop for animation
    [self.view addSubview:player];
    [player startAnimating];//start animation
    [player stopAnimating];//stop animation

Also you could use CAKeyframeAnimation. It could inform you when animation begin and end.

UIImageView *animatedImageView = [[UIImageView alloc] init]; 
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    animation.calculationMode = kCAAnimationDiscrete;
    animation.duration = 3.0f;
    animation.values = someArrayWithImages;
    animation.repeatCount = 1;
    animation.delegate = self;
    [animatedImageView.layer addAnimation:animation forKey:@"animation"];

And add delegate methods:

- (void)animationDidStart:(CAAnimation *)anim;
 - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag;
like image 20
Сергей Олейнич Avatar answered Apr 28 '26 01:04

Сергей Олейнич