【问题标题】:Bug about UIImageView display gif?关于 UIImageView 显示 gif 的错误?
【发布时间】:2017-05-15 08:22:03
【问题描述】:

我创建一个 UIImageView 并添加到 VC UIView ,然后我创建一个带有 animatedImageWithImages:duration: 的图像

当我执行代码时,它可以正常工作。它显示图像动画。但是当我推送到下一个 VC 并在向后滑动的过程中,我发现 UIImageView 什么都没有显示。 此刻手指离开屏幕,一切恢复正常。

这是我的代码:

- (void)viewDidLoad {
     [super viewDidLoad];               
     UIImageView *imgView = [[UIImageView alloc] init];
     imgView.backgroundColor = [UIColor orangeColor];
     imgView.frame = CGRectMake(20, 200, 80, 80);
     [self.view addSubview:imgView];     
     NSArray *imgs = @[[UIImage imageNamed:@"img1"],[UIImage imageNamed:@"img2"]];
     imgView.image = [UIImage animatedImageWithImages:imgs duration:0.5];
}

BugGif

我注意到 imageView 有 CAKeyframeAnimation。我猜是runmode和CAKeyframeAnimation的冲突。

SDWebImage 也有同样的问题。因为它也使用了创建 UIImage 的方法。我认为重点是animatedImageWithImages:duration:和CAKeyframeAnimation的方法。

有人知道解决办法吗?非常感谢。

手指离开屏幕的事件会使imageView正常显示。为什么以及如何解决?

GitHubBugDemo

【问题讨论】:

  • 你在 viewWillAppear 上试过了吗??
  • 尝试在 viewWillAppear 委托中加载图像,这将得到解决。
  • 它不起作用。 viewWillAppear 已运行,但在手指左屏幕上不显示任何内容。我认为关键是手指离开屏幕的那一刻会发生什么?

标签: ios uiimageview uiimage gif sdwebimage


【解决方案1】:

您可以使用 UIImageView 其他方法来设置图像和动画持续时间,如下所示

UIImageView *imgView = [[UIImageView alloc] init];
imgView.backgroundColor = [UIColor orangeColor];
imgView.frame = CGRectMake(20, 200, 80, 80);
NSArray *imgs = @[[UIImage imageNamed:@"img1"],[UIImage imageNamed:@"img2"]];
[imgView setAnimationImages:imgs];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount:HUGE_VAL];
[imgView startAnimating];
[self.view addSubview:imgView];

【讨论】:

  • 它不起作用。当我推到下一个 VC 并在向后滑动的过程中,UIImageView 什么都不显示,即使手指左屏也不会恢复正常。
  • 只需在.h文件中声明imageView对象并调用此方法[imgView startAnimating];将出现在视图中
  • 我试过了,但是失败了。 This is the bug demo 只有几行代码。
猜你喜欢
  • 2015-04-15
  • 2018-01-26
  • 1970-01-01
  • 2015-10-05
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多