简体   繁体   English

iOS-逐帧更改图像

[英]iOS - Change image frame by frame

I am trying to change an UIImageView's image frame by frame , I have several following image that should be changed by a button . 我试图逐帧更改UIImageView的图像,我有几个应该通过button更改的图像。 here is my code : 这是我的代码:

- (IBAction)next:(id)sender {

    for( i = 0; i <= 31; i++ ) {
        NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",i];
               _image.image = [UIImage imageNamed:filename];
    }

}

the problem is my codes change image to only first frame ,when I tap the button 问题是我的代码在我点击按钮时将图像更改为仅第一帧

because of your value of variable i is always 0 由于您的变量值i始终为0

Try this for solution: 尝试以下解决方案:

Create an instance variable in .h file 在.h文件中创建实例变量

int imgCount;

initialize it in ViewDidLoad method 在ViewDidLoad方法中初始化它

imgCount = 0;
NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];  // img0 is displayed
_image.image = [UIImage imageNamed:filename];


- (IBAction)next:(id)sender {
     //first (by default) img0 is display
     imgCount++;

     if (imgCount > 30) {  //suppose total image is 31 means 0 to 30
         imgCount = 0;
     }
     NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
     _image.image = [UIImage imageNamed:filename];
}

- (IBAction)previous:(id)sender {
     imgCount--;

     if (imgCount < 0) {  //suppose total image is 31 means 0 to 30
         imgCount = 30;
     }
     NSString* filename = [NSString stringWithFormat:@"pic%d.jpeg",imgCount];
     _image.image = [UIImage imageNamed:filename];
}
// create the view that will execute our animation
     UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];

     // load all the frames of our animation
     campFireView.animationImages = [NSArray arrayWithObjects:   
                                 [UIImage imageNamed:@"campFire01.gif"],
                                 [UIImage imageNamed:@"campFire02.gif"],
                                 [UIImage imageNamed:@"campFire03.gif"],
                                 [UIImage imageNamed:@"campFire04.gif"],
                                 [UIImage imageNamed:@"campFire05.gif"],
                                 [UIImage imageNamed:@"campFire06.gif"],
                                 [UIImage imageNamed:@"campFire07.gif"],
                                 [UIImage imageNamed:@"campFire08.gif"],
                                 [UIImage imageNamed:@"campFire09.gif"],
                                 [UIImage imageNamed:@"campFire10.gif"],
                                 [UIImage imageNamed:@"campFire11.gif"],
                                 [UIImage imageNamed:@"campFire12.gif"],
                                 [UIImage imageNamed:@"campFire13.gif"],
                                 [UIImage imageNamed:@"campFire14.gif"],
                                 [UIImage imageNamed:@"campFire15.gif"],
                                 [UIImage imageNamed:@"campFire16.gif"],
                                 [UIImage imageNamed:@"campFire17.gif"], nil];

     // all frames will execute in 1.75 seconds
     campFireView.animationDuration = 1.75;
     // repeat the annimation forever
     campFireView.animationRepeatCount = 0;
     // start animating
     [campFireView startAnimating];
     // add the animation view to the main window
     [self.view addSubview:campFireView];

Also download the source code form here. 也可以在此处下载源代码表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM