繁体   English   中英

动画图像到UIImageView

[英]Animated Image to UIImageView

我必须将动画图像设置为UIImageView 请参阅我想在我的UIImageView上设置的Image的示例,该UIImageView具有扩展名.GIF ,我将其转换为.PNG格式。

在此输入图像描述

我已经尝试了很多方法来做到这一点。 但没有得到预期的一个。 我需要这种技术来创建自己的自定义Progressbar 我将此图像分配给我的UIImageView 但这不是动画..如果有任何想法,那么请与我分享。

您无法在iOS设备中使用.gif图像。 不支持GIF动画。

除此之外,您可以将.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];

另一个选择是,你可以放一个UIWebView并加载你的gif

对于使用文件with.gif扩展名,您可以使用此第三方库FLAnimatedImage 这是一个很好的图书馆与演示应用程

您还可以更改一些代码,以便根据需要进行操作。 如果您有图像数组(.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

您也可以使用CAKeyframeAnimation。 它可以在动画开始和结束时通知您。

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

并添加委托方法:

- (void)animationDidStart:(CAAnimation *)anim;
 - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag;

如果你想做动画,那么你需要拍摄一组特定效果的图像然后在for循环中运行每个图像并给出动画效果。 此外,如果你仍然想做,那么你可以使用OpenGL或cocos2d框架继续前进。

但是根据iOS标准,只需保持UI清晰和平整的图像。

如果我理解正确,你有一个gif文件,并希望在UIImageView显示为动画。

这是一个流行的GitHub库,允许这样: uiimage-from-animated-gif

使用此UIImage类别,您无需将gif文件解析为多个图像。 该库负责处理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