문제

EDIT: Code altered after recommendation from @llario - no improvement in delay.

I am new to objective-C and am having some issue with an unwanted delay loading images in to an imageView. I have started a new project which only assigns an image within the IBAction so when the button is clicked the delay occurs for the image to load for the first time only. Once the images have been used once they work as i want them to - rapid! How can I remove this delay on the first load so that the images load quickly the first time too? It is possible that there will be 12 buttons on my page each with 5 images assigned, meaning 60 images.

Striped back h file:

#import <UIKit/UIKit.h>

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

- (IBAction)hippoButtonClicked:(id)sender;

@end

Striped back m file:

#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
도움이 되었습니까?

해결책

very strange problem. the last thing I can suggest is to load the UIImage in viewDidLoad.. but if you have more images this is very inconvenient. for example:

.h

#import <UIKit/UIKit.h>

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

- (IBAction)hippoButtonClicked:(id)sender;

@end

.m

#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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top