【问题标题】:Xcode: UIImageView Image Not UpdatingXcode:UIImageView 图像未更新
【发布时间】:2014-02-10 05:57:15
【问题描述】:

当按下按钮时,我试图在 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 个静态 NSString 值是否有效,而不是每次更改一个 NSString
6) 删除并重新添加图像到支持文件夹。
7) 重命名所有图像并更新代码以匹配。
8) 关闭自动布局。

任何帮助将不胜感激。

谢谢, 抢

【问题讨论】:

  • 我只能想象使用sleep() 会给您带来比帮助更多的问题。难道你不能只使用NSTimer 并安排它每秒调用setNextImage: 吗?
  • 显示你的 setImage 方法
  • 我偷偷怀疑你正在尝试使用睡眠复制动画..

标签: ios iphone objective-c xcode uiimageview


【解决方案1】:

如果您尝试更新 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]

【讨论】:

  • 非常感谢。我添加了选择下一个图像的逻辑,并从方法 buttonPressed 中删除了参数 :id(sender)。后者在执行期间导致了无效参数问题,不完全确定原因,但删除修复了错误。我的伪动画代码 (@amar) 正在运行!
【解决方案2】:

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];

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

【讨论】:

    【解决方案3】:

    试试这个

    - (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];
    }
    

    【讨论】:

    • 每次调用setNextImage: 时都会向父视图添加一个全新的UIImageView。我认为这不一定能解决问题。
    • 这将引入新问题而不是解决当前问题。
    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2015-03-14
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多