【问题标题】:UIImageView not redrawing when image set to nil图像设置为零时UIImageView不重绘
【发布时间】:2012-08-20 21:01:29
【问题描述】:

我正在使用UIImageView 来显示静止图像和动画图像。所以有时,我使用.image,有时我使用.animationImages。这很好。

无论是静态的还是动画的,我都将UIImages 存储在item.frames 中(见下文)

问题是我想在加载动画帧时在视图中心显示一个UIActivityIndicatorView。我希望在没有图像或框架的情况下发生这种情况。没有做它应该做的那一行是:

[self.imageView removeFromSuperview];

事实上,此时将其设置为另一个图像也无济于事。似乎这里没有发生任何 UI 内容。顺便说一句,

NSLog(@"%@", [NSThread isMainThread]?@"IS MAIN":@"IS NOT");

打印IS MAIN。

那里的图像会一直存在,直到新的动画帧都在那里(1-2 秒)并且它们开始动画

这一切都是从 UIView 的子类运行的,UIImageView 作为子视图。

- (void)loadItem:(StructuresItem *)item{

    self.imageView.animationImages = nil;
    self.imageView.image = nil;   

    [self.spinner startAnimating];

    self.item = item;

    if (item.frameCount.intValue ==1){
        self.imageView.image = [item.frames objectAtIndex:0];
        self.imageView.animationImages = nil;
    }else {
        [self.imageView removeFromSuperview];

        self.imageView =[[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:self.imageView ];
        if( self.imageView.isAnimating){
            [self.imageView stopAnimating];
        }
        self.imageView.animationImages = item.frames;
        self.imageView.animationDuration = self.imageView.animationImages.count/12.0f;

        //if the image doesn't loop, freeze it at the end
        if (!item.loop){
            self.imageView.image = [self.imageView.animationImages lastObject];
            self.imageView.animationRepeatCount = 1;
        }
        [self.imageView startAnimating];
    }

    [self.spinner stopAnimating];

}

我无知的评估是,一旦该图像设置为零,就不会重绘某些东西。很想帮忙。

【问题讨论】:

  • 加载动画帧是否会阻塞您的主线程?
  • 我就是这么想的。我该怎么做?
  • 我猜只是使用 GCD 将耗时的部分扔到另一个线程中?
  • 这似乎是我了解 GCD 的一个很好的借口。谢谢。

标签: ios multithreading cocoa-touch uiimageview uiimage


【解决方案1】:

我发现的不是问题的答案,而是解决问题的更好方法。简单地说,使用 NSTimer 而不是 animationImages。它加载速度更快,不会耗尽内存并且代码更简单。耶!

这样做:

-(void)stepFrame{
    self.currentFrameIndex =   (self.currentFrameIndex + 1) % self.item.frames.count;
    self.imageView.image = [self.item.frames objectAtIndex:self.currentFrameIndex];
}

还有这个

-(void)run{
    if (self.item.frameCount.intValue>1){
        self.imageView.image = [self.item.frames objectAtIndex:self.currentFrameIndex];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1/24.0f target:self selector:@selector(stepFrame) userInfo:nil repeats:YES];
    }else{
        self.imageView.image = [self.item.frames objectAtIndex:0];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多