Xcode:UIImageView图像未更新

时间:2014-02-10 05:57:15

标签: ios iphone objective-c xcode uiimageview

我正在尝试在按下按钮时在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)产品&gt;清洁
2)重新安装app 3)检查所有区分大小写。
4)设置了多个设置UIImageView图像的方法 5)检查34个静态NSString值是否有效,而不是每次更改一个NSString 6)删除并重新添加图像到支持文件夹 7)重命名所有图像和更新的代码以匹配 8)关闭AutoLayout。

任何帮助都会非常感激。

谢谢, 罗布

3 个答案:

答案 0 :(得分:0)

如果您尝试更新用户界面,我认为您通常希望避免使用sleep()。当您调用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]

答案 1 :(得分:0)

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

您必须按顺序重命名图像,然后按下按钮调用..

答案 2 :(得分:-1)

试试这个

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