Sarat Patel
Sarat Patel

Reputation: 856

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.

Upvotes: 3

Views: 3899

Answers (4)

Midhun MP
Midhun MP

Reputation: 107131

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.

Upvotes: 5

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;

Upvotes: 1

aytunch
aytunch

Reputation: 1434

If I understood you correctly, you have a gif file and want to display it in an UIImageView as animated.

Here is a popular GitHub Library which allows this: uiimage-from-animated-gif

With this UIImage category, you won't need to parse the gif files to several images. This library takes care of the single images inside of the gif file and the durations for each image to be displayed for you.

NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];
self.myImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];

Upvotes: 0

Pallavi Ligade
Pallavi Ligade

Reputation: 529

If you wanted to do animation then you need to take set of images of one particular that effect then run each images in for loop and give animation effect. Also if you still wanted to do then you can use OpenGL or cocos2d framework to go forward.

But as per iOS Standard just keep your UI clean and flat images.

Upvotes: 0

Related Questions