1

I have a looping animation consisting of 120 frames, 512x512 resolution, saved as 32bit PNG files. I want to play this sequence back in a UIView inside my application. Can anyone give me some pointers regarding how I might do this, hopefully I can do this using the standard API (which I would prefer). I could use Cocos2D if needed or even OpenGL (but I am totally new to OpenGL at this point).

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

2 Answers2

6

You can try this:

// Init an UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:/*Some frame*/];

// Init an array with UIImage objects
NSArray *array = [NSArray arrayWithObjects: [UIImage imageNamed:@"image1.png"], [UIImage imageNamed:@"image2.png"], .. ,nil];

// Set the UIImage's animationImages property
imageView.animationImages = array;

// Set the time interval
imageView.animationDuration = /* Number of images x 1/30 gets you 30FPS */;

// Set repeat count 
imageView.animationRepeatCount = 0; /* 0 means infinite */

// Start animating
[imageView startAnimating];

// Add as subview
[self.view addSubview:imageView];

This is the easiest approach, but I can't say anything about the performance, since I haven't tried it. I think it should be fine though with the images that you have.

iska
  • 2,208
  • 1
  • 18
  • 37
  • I think you'll run out of memory trying to create the array of images. – picciano Feb 17 '12 at 18:02
  • Just out of curiosity I made a small project. I used 120 PNGs each is 512x512 pixels and about 790KB in size, so overall 95MB. It took some time to load, but then ran just fine. Tried it on the 4.3 Simulator, the 5.0 Simulator and on an iPhone 4S. The simulators didn't even show memory warnings. – iska Feb 17 '12 at 19:13
  • Testing on (eg) a relatively unencumbered 4S doesn't compare to a bloated 4; several of my clients have way worse memory problems than I ever do. Before committing to such a solution I recommend trying this on your lowest target device; 3G phones and 2G touches, for example, only have 128MB of RAM, and ~95MB is a lot to consume even for the 256MB in newer devices (3GS phone, 4G touch). – AndrewS Feb 17 '12 at 20:09
  • This approach will crash when run on the device. You simply cannot decode that many images into memory. – MoDJ Jun 19 '13 at 01:45
1

Uncompressed, that's about 90MB of images, and that might be as much as you're looking at if they're unpacked into UIImage format. Due to the length of the animation and the size of the images, I highly recommend storing them in a compressed movie format. Take a look at the reference for the MediaPlayer framework; you can remove the playback controls, embed an MPMoviePlayerController within your own view hierarchy, and set playback to loop. Note that 640x480 is the upper supported limit for H.264 so you might need to scale down the video anyway.

Do take a note of issues looping video, as mentioned in the question here Smooth video looping in iOS.

Community
  • 1
  • 1
AndrewS
  • 8,196
  • 5
  • 39
  • 53
  • True, but if he is going to go through the hassle of converting to a movie, implementing the `MPMoviePlayer` and probably scaling the movie down, he can first try to scale down the images to the exact needed size and/or compressing them, write seven lines of code and see what happens ;) – iska Feb 17 '12 at 17:23
  • @iska: agreed, it's worth testing a simple solution; see my note on your answer. – AndrewS Feb 17 '12 at 20:10