简体   繁体   English

动画图像到UIImageView

[英]Animated Image to UIImageView

I have to set an animated Image to UIImageView . 我必须将动画图像设置为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. 请参阅我想在我的UIImageView上设置的Image的示例,该UIImageView具有扩展名.GIF ,我将其转换为.PNG格式。

在此输入图像描述

I already tried too many way to do it. 我已经尝试了很多方法来做到这一点。 but not get expected one. 但没有得到预期的一个。 I need this technique to create my own custom Progressbar . 我需要这种技术来创建自己的自定义Progressbar I assign this image to my UIImageView . 我将此图像分配给我的UIImageView but this is not animating.. If any one have any idea then please share it with me. 但这不是动画..如果有任何想法,那么请与我分享。

You can't use .gif images in iOS devices. 您无法在iOS设备中使用.gif图像。 GIF animations not supported. 不支持GIF动画。

Instead of that you can add each frame of that .gif as a .png or .jpg image and you can animate it by code. 除此之外,您可以将.gif每个帧添加为.png.jpg图像,您可以通过代码为其设置动画。

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. 另一个选择是,你可以放一个UIWebView并加载你的gif

For use file with.gif extension you can use this third-part library FLAnimatedImage . 对于使用文件with.gif扩展名,您可以使用此第三方库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: 如果您有图像数组(.png或其他扩展名),您可以使用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. 您也可以使用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;

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. 如果你想做动画,那么你需要拍摄一组特定效果的图像然后在for循环中运行每个图像并给出动画效果。 Also if you still wanted to do then you can use OpenGL or cocos2d framework to go forward. 此外,如果你仍然想做,那么你可以使用OpenGL或cocos2d框架继续前进。

But as per iOS Standard just keep your UI clean and flat images. 但是根据iOS标准,只需保持UI清晰和平整的图像。

If I understood you correctly, you have a gif file and want to display it in an UIImageView as animated. 如果我理解正确,你有一个gif文件,并希望在UIImageView显示为动画。

Here is a popular GitHub Library which allows this: uiimage-from-animated-gif 这是一个流行的GitHub库,允许这样: uiimage-from-animated-gif

With this UIImage category, you won't need to parse the gif files to several images. 使用此UIImage类别,您无需将gif文件解析为多个图像。 This library takes care of the single images inside of the gif file and the durations for each image to be displayed for you. 该库负责处理gif文件中的单个图像以及为您显示的每个图像的持续时间。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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