Objective c 在iAction内的ImageView中声明图像时出现不必要的延迟

Objective c 在iAction内的ImageView中声明图像时出现不必要的延迟,objective-c,uiimageview,delay,ibaction,Objective C,Uiimageview,Delay,Ibaction,编辑:在@llario推荐后更改代码-延迟没有改善 我是objective-C新手,在将图像加载到imageView时遇到了一些不必要的延迟问题。我已经开始了一个新的项目,它只在iAction中分配一个图像,因此,当单击按钮时,仅第一次加载图像时会出现延迟。一旦图像被使用,一旦它们按照我希望的那样工作-快速!如何在第一次加载时消除此延迟,以便图像在第一次加载时也能快速加载?我的页面上可能有12个按钮,每个按钮分配了5个图像,即60个图像 条带化回h文件: #import <UIKit/UI

编辑:在@llario推荐后更改代码-延迟没有改善

我是objective-C新手,在将图像加载到imageView时遇到了一些不必要的延迟问题。我已经开始了一个新的项目,它只在iAction中分配一个图像,因此,当单击按钮时,仅第一次加载图像时会出现延迟。一旦图像被使用,一旦它们按照我希望的那样工作-快速!如何在第一次加载时消除此延迟,以便图像在第一次加载时也能快速加载?我的页面上可能有12个按钮,每个按钮分配了5个图像,即60个图像

条带化回h文件:

#import <UIKit/UIKit.h>

@interface FCV2ViewController : UIViewController
{
    UIImageView *animalImage;
    int imageNumber;
}

- (IBAction)hippoButtonClicked:(id)sender;

@end

非常奇怪的问题。我建议的最后一件事是在viewDidLoad中加载UIImage。。但是如果你有更多的图像,这是非常不方便的。例如:

h


这个IBAction很好用吗?因为我认为通过这种方式,点击时int imageNumber将永远为0。。您每次单击时都会声明一个int=0。。我知道这不是问题,它是有效的,它在图像中循环,所以我想这不是问题,除非你觉得它会更好地放在其他地方?你在使用故事板吗?是的,我认为int应该在.h中声明,在viewDidLoad set imageNumber=0中声明,而不是在IBActionOK中声明。谢谢,我已经在h中设置了int,并更改了m以在viewDidLoad中设置int。不过,这对应用程序的运行方式或我遇到的问题没有任何影响-无论如何,谢谢。你使用故事板吗?因为如果你已经在你的viewController中连接了故事板中的IBOutlet,你就不需要[self.view addSubview:animalage]嘿,好吧,这会帮你很大的忙,而且会大大加快速度。我不得不说,虽然每个图像的第一次加载速度仍然不如之后的加载速度快。谢谢你的帮助。你知道为什么没有这么快吗?
#import "FCV2ViewController.h"

@interface FCV2ViewController ()

@end

@implementation FCV2ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    imageNumber = 0;
    animalImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 400, 300, 300)];
    [self.view addSubview:animalImage];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)hippoButtonClicked:(id)sender
{

    if (imageNumber == 0) {
        animalImage.image = [UIImage imageNamed:@"hippo-00"];
        imageNumber++;
    }
    else if (imageNumber == 1) {
        animalImage.image = [UIImage imageNamed:@"hippo-01"];
        imageNumber = 0;
    }
}

@end
#import <UIKit/UIKit.h>

@interface FCV2ViewController : UIViewController
{
 UIImageView *animalImage;
 int imageNumber;
 UIImage *hippo00;
 UIImage *hippo01;
}

- (IBAction)hippoButtonClicked:(id)sender;

@end
#import "FCV2ViewController.h"

@interface FCV2ViewController ()

@end

@implementation FCV2ViewController

- (void)viewDidLoad
 {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  imageNumber = 0;
  animalImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 400, 300, 300)];
  [self.view addSubview:animalImage];

  hippo00 = [UIImage imageNamed:@"hippo-00.png"];
  hippo01 = [UIImage imageNamed:@"hippo-01.png"];

 }

- (void)didReceiveMemoryWarning
 {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
 }

- (IBAction)hippoButtonClicked:(id)sender
 {

  if (imageNumber == 0) {
     animalImage.image = hippo00;
     imageNumber++;
  }
  else if (imageNumber == 1) {
     animalImage.image = hippo01;
     imageNumber = 0;
  }
 }

@end