What I basically want to do is tp display a sequence of images at a fast rate (60/second) in order to create a sequence. I’m going to create a timer and see if it’s fast enough.
I want to display an array of images in my cocoa app using an animation (up to 60 per seconds).
Here’s the animation I’ve created so far:
- (CAAnimation*)imageAnimation;
{
CGImageRef image1 = [self nsImageToCGImageRef:[NSImage imageNamed:@"1.png"]];
CGImageRef image2 = [self nsImageToCGImageRef:[NSImage imageNamed:@"2.png"]];
CGImageRef image3 = [self nsImageToCGImageRef:[NSImage imageNamed:@"3.png"]];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSArray *images = [NSArray arrayWithObjects:(id)image1, (id)image2, (id)image3, nil];
[animation setValues:images];
[animation setDuration:3.0];
return animation;
}
where nsImageToCGImageRef is a method I’ve defined elsewhere. Now, I’m not sure where to go from here to add this animation to my views.
You should investigate
CVDisplayLink, which allows you to receive a callback every time the Mac’s display subsystem is about to display a frame. This will let you display images at 60fps with no dropped frames, because the API only calls your code when it needs an update.This means you don’t need to worry about synchronising a timer with the display, and it also means that you are not doing unnecessary work. You can also do things such as update only on alternate callbacks to reduce the frame rate to 30fps if your drawing code is slow.