0

I have an app that uses the camera, with a gun image overlaid as well as a fire and reload button. On the retina iPad and iPhone, it cycles through 30 images in about a second for the firing animation, and 41 in 2 seconds for the reload animation. All of the images are 1920 x 1080, and on the iPhone they are 1000 x 533. To cycle through the .pngs when the fire button is tapped for example, I am using this in the PlayViewController.m file:

- (IBAction)fire:(id)sender {

// Play the firing animation for the rifle, enable reload button

fireButton.enabled = NO;
type.animationImages  = gunanimload;
type.animationDuration = 1.0;
type.animationRepeatCount = 1;
reloadButton.enabled = YES;
[type startAnimating];

In the viewDidLoad method I create the array and load it with images:

- (void)viewDidLoad
{
[super viewDidLoad];

UIImage* img1 = [UIImage imageNamed:@"rev0001.png"];

... x30

gunanimload = [NSArray arrayWithObjects:img1, ... x30, nil];

}

Even though it should be loading the array when the view loads, it still seems to be doing it when the Fire button is tapped. How would I eliminate this delay? Or is there a (relatively simple) alternative to playing the fire and reload animations?

Dale Townsend
  • 671
  • 3
  • 13
  • 25

1 Answers1

1

The UIImage imageNamed method will not load the image until it is needed for display. There are methods to pre-load the images upfront.

Read this for more information: CGImage/UIImage lazily loading on UI thread causes stutter

Non-lazy image loading in iOS

Community
  • 1
  • 1
Resh32
  • 6,500
  • 3
  • 32
  • 40
  • Also consider using sprites instead of this image array. It will increase performance and reduce memory requirements. – Resh32 Sep 04 '12 at 11:43
  • Read this for a good example of sprites: http://mysterycoconut.com/blog/2011/01/cag1/ – Resh32 Sep 04 '12 at 11:44
  • You are not correct about the imageNamed method, it loads right away and caches the image data in memory. The correct approach is to decode one frame at a time in a timer when the frames change or to decode all the frames to raw data before hand and then load from mapped memory. See more about this issue in my answer here: http://stackoverflow.com/questions/8112698/how-to-do-animations-using-images-efficiently-in-ios/8113027#8113027 – MoDJ Nov 28 '14 at 09:26