Animation using array of images in sequence

ios image animation effect
uiimageview

I have an array of images which I want to animate by playing these images one after the other in a sequence. I want to repeat the whole loop several times. I am developing a game for iPad. Suggest to me a method to achieve this functionality in Objective-C with the Cocoa framework.

See the animationImages property of UIImageView. It’s hard to say if it fits your needs as you don’t give us details, but it’s a good start.

swift Animation using array of images in sequence , Animation using array of images in sequence in swift. iOS에서 간단히 애니메이션을 구현하는 방법 // swift let animationImages:[AnyObject]  Create a multidimensional array by concatenating the individual images of an image sequences. Each image must be the same size and have the same number of color channels. If you are storing a sequence of indexed images, each image must use the same color map.

NSArray *animationArray = [NSArray arrayWithObjects:
                          [UIImage imageNamed:@"images.jpg"],
                          [UIImage imageNamed:@"images1.jpg"],
                          [UIImage imageNamed:@"images5.jpg"],
                          [UIImage imageNamed:@"index3.jpg"],
                          nil];
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor      = [UIColor purpleColor];
animationView.animationImages      = animationArray;
animationView.animationDuration    = 1.5;
animationView.animationRepeatCount = 0;
[animationView startAnimating]; 
[self.view addSubview:animationView];       
[animationView release];

add your own images in the array.repeat Count 0 means infinite loop.You can give your own number also.

10.3: An Array of Images - Processing Tutorial, Book: Learning Processing A Beginner's Guide to Programming, Images,​Animation Duration: 11:18 Posted: Jul 24, 2015 Image Sequence GIF-like behaviour controlled by the scroll bar. Animating through image sequences can be achieved by creating a tween that cycles through an array of images (like shown here). A different approach would be to do it without TweenMax and just update the image using the scene progress event.

There are at least 3 ways to animate an array of images through a UIImageView. I'm adding 3 links to download sample code for the 3 possibilities.

The first one is the one that everyone knows. The other ones are less known.

- UIImageView.animationImages Example Link

The problem of this one is that do not have Delegate to tell us in which moment the animation is finished. So, we can have problems if we want to display something after the animation.

In the same way, there is no possibility to kept the last image from the animation in the UIImageView automatically. If we combine both problems we can have a gap at the end of the animation if we want to kept the last frame on screen.

self.imageView.animationImages = self.imagesArray; // the array with the images
self.imageView.animationDuration = kAnimationDuration; // static const with your value
self.imageView.animationRepeatCount = 1;
[self.imageView startAnimating];

- CAKeyframeAnimation Example Link

This way to animate works through CAAnimation. It have an easy delegate to use and we can know when the animation finish.

This is probably the best way to animate an array of images.

- (void)animateImages
{
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    keyframeAnimation.values = self.imagesArray;

    keyframeAnimation.repeatCount = 1.0f;
    keyframeAnimation.duration = kAnimationDuration; // static const with your value

    keyframeAnimation.delegate = self;

    //    keyframeAnimation.removedOnCompletion = YES;
    keyframeAnimation.removedOnCompletion = NO;
    keyframeAnimation.fillMode = kCAFillModeForwards;

    CALayer *layer = self.animationImageView.layer;

    [layer addAnimation:keyframeAnimation
                 forKey:@"girlAnimation"];
}

Delegate:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (flag)
    {
        // your code
    }
}

- CADisplayLink Example Link

A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.

This way to do it is really interesting and opens a lot of possibilities to manipulate what are we showing in screen.

DisplayLink getter:

- (CADisplayLink *)displayLink
{
    if (!_displayLink)
    {
        _displayLink = [CADisplayLink displayLinkWithTarget:self
                                                   selector:@selector(linkProgress)];
    }

    return _displayLink;
}

Methods:

- (void)animateImages
{
    self.displayLink.frameInterval = 5;
    self.frameNumber = 0;
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop]
                           forMode:NSRunLoopCommonModes];
}

- (void)linkProgress
{
    if (self.frameNumber > 16)
    {
        [self.displayLink invalidate];
        self.displayLink = nil;
        self.animationImageView.image = [UIImage imageNamed:@"lastImageName"];
        self.imagesArray = nil;
        return;
    }

    self.animationImageView.image = self.imagesArray[self.frameNumber++];
    self.frameNumber++;
}

GENERAL PROBLEM:

Even though we have this 3 possibilities, if your animation is with a lot of big images, consider using a video instead. The usage of memory will decrease a lot.

A General problem you will face doing this is in the moment of the allocation of the images.

If you use [UIImage imageNamed:@"imageName"] you will have cahe problems.

From Apple:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method locates and loads the image data from disk or asset catelog, and then returns the resulting object. You can not assume that this method is thread safe.

So, imageNamed: stores the image in a private Cache. - The first problem is that you can not take control of the cache size. - The second problem is that the cache did not get cleaned in time and if you are allocating a lot of images with imageNamed:, your app, probably, will crash.

SOLUTION:

Allocate images directly from Bundle:

NSString *imageName = [NSString stringWithFormat:@"imageName.png"];
NSString *path = [[NSBundle mainBundle] pathForResource:imageName

// Allocating images with imageWithContentsOfFile makes images to do not cache.
UIImage *image = [UIImage imageWithContentsOfFile:path];

Small problem:

Images in Images.xcassets get never allocated. So, move your images outside Images.xcassets to allocate directly from Bundle.

Images Array, Concept: Simple Animations. Animations in games are represented as a series of different images that get shown in a sequence. Using ||arrays:arrays|| ,  Controlling images via Javascript Stephen Wilson, Conceptual Design, SFSU Navigator 3.0 and above introduced the Image object that can be controlled by Javascript. All images on a web page form part of an Image array. For example, the first image on a page is Image[0] and the second is Image[1].

I have added a swift 3.0 extension for this

extension UIImageView {

    func animate(images: [UIImage], index: Int = 0, completionHandler: (() -> Void)?) {

        UIView.transition(with: self, duration: 0.5, options: .transitionCrossDissolve, animations: {
            self.image = images[index]

        }, completion: { value in
            let idx = index == images.count-1 ? 0 : index+1

            if idx == 0 {
                completionHandler!()

            } else {
                self.animate(images: images, index: idx, completionHandler: completionHandler)
            }

        })
    }

}

Beginning iOS Game Development, Animating a sequence of images with UIImageView • Learning the basic uses for Core You provide an array of images to the image view, set up the animation  How can I animate a sequence of images (say Frame-1.png all the to Frame-6) using the SwiftUI framework? I've tried creating an array of "images". Then I assigned the UIImage.animatedImage(with: i

Best solution for me use CADisplayLink. UIImageView doesn't have completion block and you can't catch steps of animation. In my task i must changing background of view with image sequencer step by step. So CADisplayLink allows you handling steps and finishing animation. If we talk about usage of memory, i think best solution load images from bundle and delete array after finishing

ImageSequencer.h

typedef void (^Block)(void);

@protocol ImageSequencerDelegate;

@interface QSImageSequencer : UIImageView
@property (nonatomic, weak) id <ImageSequencerDelegate> delegate;

- (void)startAnimatingWithCompletionBlock:(Block)block;
@end

@protocol ImageSequencerDelegate <NSObject>
@optional
- (void)animationDidStart;
- (void)animationDidStop;
- (void)didChangeImage:(UIImage *)image;
@end

ImageSequencer.m

- (instancetype)init {
    if (self = [super init]) {

        _imagesArray = [NSMutableArray array];

        self.image = [self.imagesArray firstObject];
    }

    return self;
}


#pragma mark - Animation

- (void)startAnimating {
    [self startAnimatingWithCompletionBlock:nil];
}


- (void)startAnimatingWithCompletionBlock:(Block)block {
    self.frameNumber = 0;

    [self setSuccessBlock:block];

    self.displayLink.frameInterval = 5;

    if (self.delegate && [self.delegate respondsToSelector:@selector(animationDidStart)]) {
        [self.delegate animationDidStart];
    }

    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop]
                           forMode:NSRunLoopCommonModes];
}


-(void)stopAnimating {
     self.image = [self.imagesArray lastObject];

    [self.displayLink invalidate];
    [self setDisplayLink:nil];
    Block block_ = [self successBlock];
    if (block_) {
        block_();
    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(animationDidStop)]) {
        [self.delegate animationDidStop];
    }

    [self.imagesArray removeAllObjects];
}


- (void)animationProgress {
    if (self.frameNumber >= self.imagesArray.count) {
        [self stopAnimating];
        return;
    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(didChangeImage:)]) {
        [self.delegate didChangeImage:self.imagesArray[self.frameNumber]];
    }

    self.image = self.imagesArray[self.frameNumber];
    self.frameNumber++;
}


#pragma mark - Getters / Setters

- (CADisplayLink *)displayLink {
    if (!_displayLink){
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animationProgress)];
    }

    return _displayLink;
}

- (NSMutableArray<UIImage *> *)imagesArray {
    if (_imagesArray.count == 0) {
        // get images from bundle and set to array
    }
    return _imagesArray;
}

@end

Can I use an image sequence with offset frames as a texture on , I'm creating an animation of a film bed with a film strip running through it. The film strip is basically just an array object with a curve modifier. What I want to do is  We will need to store our images an array in order to easily reference them for our animation. Define an instance variable for your images array. Note: images is plural because it will contain multiple images. [UIImage]! means it will contain a collection of items, each item being of type, UIImage. var images: [UIImage]!

Sequential \ Examples \ Processing.org, Displaying a sequence of images creates the illusion of motion. The number of frames in the animation int currentFrame = 0; PImage[] images = new draw() { background(0); currentFrame = (currentFrame+1) % numFrames; // Use % to  We will need to store our images an array in order to easily reference them for our animation. Define an instance variable for your images array. Note: images is plural because it will contain multiple images. [UIImage]! means it will contain a collection of items, each item being of type, UIImage.

Learn Pixi.js, Instead of just using one texture, a MovieClip is a sprite that uses an array of Figure 5-1 shows a PNG tileset image with a three-frame animation sequence of​  Concept: Remainder Operator. Creating an animation using arrays is useful, but does lead to some issues. For example, what if the intention is to run it in the background, forever?

Complex animation sequences, Angular also lets you animate coordinated sequences, such as an entire grid or list of Animate multiple elements using query() and stagger() functionslink with query() to find inner elements, for example gathering all images within a <div​> . Convert between video files and sequences of image files using VideoReader and VideoWriter. The sample file named shuttle.avi contains 121 frames. Convert the frames to image files using VideoReader and the imwrite function. Then, convert the image files to an AVI file using VideoWriter. Create a temporary working folder to store the image

Comments
  • what kind of details will be helpful??
  • What kind of game? Action, desk? (That makes a difference in the expected animation performance.) How many images?
  • its a small slots game that i m building,i have 13-20 images in an array,which will be played after certain conditions are met in the game,and at time 5(at the max,min 3) arrays of this type will be functional(animating),if you wanto to knw anything more plz lemme knw!
  • Sounds reasonable, I’d try the UIImageView.
  • ok ill try that.is QTmovie will be helpful,if i could make a video out of these array,as mentioned stackoverflow.com/questions/3592167/…
  • @Gypsa can we add pageControl?
  • Thanks easy and effective.