2

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

无论是静态的还是动画的,我都将UIImages's 存储在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];

}

我无知的评估是,一旦该图像设置为零,就不会重绘某些东西。会喜欢一只手。

4

1 回答 1

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];
    }
}
于 2012-08-21T15:56:15.107 回答