Ios UIImageView在图像设置为nil时不重新绘制
我正在使用Ios UIImageView在图像设置为nil时不重新绘制,ios,multithreading,cocoa-touch,uiimageview,uiimage,Ios,Multithreading,Cocoa Touch,Uiimageview,Uiimage,我正在使用UIImageView来显示静态图像和动画图像。所以有时候,我使用.image,有时候我使用.animationImages。这很好 无论是静态还是动画,我都将UIImages存储在item.frames中(见下文) 问题是加载动画帧时,我希望在视图的中心显示UIActivityIndicatorView。我希望这种情况发生时没有图像或帧在那里。这条线没有做它应该做的是: [self.imageView removeFromSuperview]; 事实上,此时将其设置为另一个图像也没
UIImageView
来显示静态图像和动画图像。所以有时候,我使用.image
,有时候我使用.animationImages
。这很好
无论是静态还是动画,我都将UIImages
存储在item.frames
中(见下文)
问题是加载动画帧时,我希望在视图的中心显示UIActivityIndicatorView
。我希望这种情况发生时没有图像或帧在那里。这条线没有做它应该做的是:
[self.imageView removeFromSuperview];
事实上,此时将其设置为另一个图像也没有任何作用。这里似乎没有发生任何UI方面的事情。顺便说一句
NSLog(@"%@", [NSThread isMainThread]?@"IS MAIN":@"IS NOT");
打印是主要的
其中的图像将一直保留,直到新的动画帧都在其中(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];
}
我无知的评估是,一旦图像设置为零,就不会重新绘制某些内容。我很想帮忙。我发现这不是问题的答案,而是解决问题的更好方法。简单地说,使用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];
}
}
有没有可能加载动画帧会阻塞主线程?我就是这么想的。我该怎么做呢?我想用GCD把耗时的部分放到另一个线程中去吧?这看起来是我学习GCD的好借口。谢谢