Xcode: UIImageView Image Not Updating



我试图在按下按钮时在UIImageView容器中显示34个连续的图像。

- (IBAction)buttonPressed {
    [self updateUI:(0)];
}
-(void)updateUI:(int)yesser{
    //if GO is pressed
        //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files
        for(int j = 0;j<34;j++){
            NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i.PNG",42+j];
            NSLog(@"%@",newPhoto);
            sleep(1);
            [self setNextImage:newPhoto];
        }
}
-(void)setNextImage:(NSString*)nextImage{
    [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES];
}

但是,图像不会更新。当所有代码完成并返回时,将显示第一个图像。

我尝试了以下方法:
1) 产品>清洁
2)重新安装的应用程序。
3) 检查了所有区分大小写。
4) 测试了设置 UIImageView 图像的方法数量。
5) 检查 34 个静态 NS 值是否有效,而不是每次更改
一个 NSString6) 删除并重新添加图像到支持文件文件夹。
7)重命名所有图像并更新代码以匹配。
8) 关闭自动布局。

任何帮助将不胜感激。

谢谢抢

我认为如果您尝试更新 UI,您通常希望避免使用 sleep()。当您调用睡眠时,您告诉主线程暂停一段时间。由于 UI 都是在主线程上完成的,我可以想象,当它尝试更新其视图时,您对 sleep() 的调用会阻止它完全更新。因此,为什么您可能直到最后才看到任何更新。

正如我在评论中提到的,由于您想更新我想象中的UIImageView,您应该考虑使用NSTimer。此类对于想要按特定时间间隔计划定期更新非常方便。在这种情况下,我们希望UIImageView开始更新调用buttonPressed的分钟数,因此我们可以执行以下操作:

- (IBAction)buttonPressed:(id)sender {
    [self updateUI];
}
- (void)updateUI{
    // If you wanted to keep track of this NSTimer, you could hold onto it 
    // via a property.
    // `fire` basically tells the timer to begin executing what's been set 
    // for the selector.
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setNextImage:) userInfo:nil repeat:YES];
    [timer fire];
}
- (void)setNextImage {
        // Here is where your logic would change. You would need some kind of way 
        // of figuring out which image to display. You could have an NSInteger 
        // property store how many times the method gets called and then 
        // determine which image to show from there, for example.
        // Assume that with your new logic, you have acquired the image you 
        // want to display.
        UIImage *image = imageToDisplay;
        // Also assuming here that (based on your code), `ball` is the 
        // name of your UIImageView.
        ball.image = imageToDisplay;
}

要销毁计时器,只需调用 [timer invalidate]

UIImageView有一个用于动画图像的选项,试试这个

imageView.animationImages = [NSArray arrayWithObjects:
                                        [UIImage imageNamed:@"progress-1"],
                                        [UIImage imageNamed:@"progress-2"],
                                        [UIImage imageNamed:@"progress-3"],
                                        [UIImage imageNamed:@"progress-4"], nil];
imageView.animationDuration = 2.0f;
imageView.animationRepeatCount = 1;
[imageView startAnimating];

您必须按顺序重命名图像,并在按下按钮时调用它。

试试这个

- (IBAction)buttonPressed {
    [self updateUI:(0)];
}
-(void)updateUI:(int)yesser{
//if GO is pressed
    //display 34 images MG_8842.PNG -> MG_8875.PNG stored in Supporting Files
    for(int j = 0;j<34;j++){
        NSString *newPhoto =[NSString stringWithFormat:@"MG_88%i",42+j];
        NSLog(@"%@",newPhoto);
        sleep(1);
        [self setNextImage:newPhoto];
    }
}
-(void)setNextImage:(NSString*)nextImage{
     [ball performSelectorOnMainThread:@selector(setImage:) withObject: [UIImage imageNamed:nextImage] waitUntilDone:YES];
     UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
     imgView.image = [UIImage imageNamed:[NSString stringWithFormat: @"%@.png", nextImage]];
    [self.view addSubview:imgView];
}

最新更新