【问题标题】:Animated Image to UIImageView动画图像到 UIImageView
【发布时间】:2014-12-29 10:44:27
【问题描述】:

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

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

【问题讨论】:

  • 一点,这张图已经是动画类型了……

标签: ios objective-c ipad uiimageview uiviewanimation


【解决方案1】:

您不能在 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

【讨论】:

  • 是的,但是我将它转换为 .png 然后我们可以使用它。但没有达到我的预期。
  • 这很简单。但是对于这种方法,我必须在我的包中添加很多 Images。这将增加我的应用程序大小。这仍然不会像上面的屏幕截图那样平滑。你遇到了我的实际问题 - @Midhun MP
  • @Sarat_Patel:是的,我收到了你的问题。另一种方法是放置一个 UIWebView 并在其中加载 gif 图像。
【解决方案2】:

对于使用.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;

【讨论】:

  • 请注意:如果你使用CAKeyFrameAnimation,数组的类型应该是CGImage
【解决方案3】:

如果你想做动画,那么你需要拍摄一组具有特定效果的图像,然后在 for 循环中运行每个图像并赋予动画效果。 另外如果你还想做的话,可以使用OpenGL或者cocos2d框架来进行。

但根据 iOS 标准,只需保持 UI 干净和扁平的图像即可。

【讨论】:

    【解决方案4】:

    如果我理解正确,您有一个 gif 文件并希望将其以动画形式显示在 UIImageView 中。

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

    使用此UIImage 类别,您无需将 gif 文件解析为多个图像。该库负责处理 gif 文件中的单个图像以及为您显示每个图像的持续时间。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多