Question

I have a 15 second animation in my app that is currently way too inefficient to be workable as it uses 1.5GB of RAM.

Am I using the animation properties of UIImageView incorrectly? Perhaps there is a different solution. All I want is for it to loop for a certain duration.

There will be improvements I can make to the 539 images to make them more efficient, but they still need to be retina size etc. Currently it is 30fps.

- (void)viewDidLoad {

    [super viewDidLoad];

    _animationFrames = [[NSMutableArray alloc] init];

    for (long l = 0; l < 540; l++) {

        NSString *frameNumber = [NSMutableString stringWithFormat:@"AnimationFrames-%04ld", l];

        UIImage *frame = [UIImage imageNamed:frameNumber];

        [_animationFrames addObject:frame];
    }

    UIImageView *animation = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    animation.animationImages = _tutorialFrames;
    animation.animationDuration = 14;

    [self.view addSubview: animation];
    [animation startAnimating];
}
Was it helpful?

Solution

Thanks to Mirko Brunner's comments I discovered that I could use an embedded movie rather than play the animation through UIImageView, though I ended up going with AVPlayer rather than MPMoviePlayer.

#import "VideoViewController.h"

@import AVFoundation;

@interface VideoViewController ()

@property (weak, nonatomic) AVPlayer *avPlayer;
@property (weak, nonatomic) AVPlayerLayer *avPlayerLayer;

@end

@implementation VideoViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self avPlayer];
    [self.avPlayer play];

}

- (AVPlayer *)avPlayer {

    if (!_avPlayer) {

        NSURL *url = [[NSBundle mainBundle]
                        URLForResource: @"Video" withExtension:@"mp4"];
        _avPlayer = [AVPlayer playerWithURL:url];
        _avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:_avPlayer];

        _avPlayerLayer.frame = self.view.layer.bounds;
        [self.view.layer addSublayer: _avPlayerLayer];

        _avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[_avPlayer currentItem]];
    }
    return _avPlayer;
}

- (void)playerItemDidReachEnd:(NSNotification *)notification {  

    AVPlayerItem *p = [notification object];
    [p seekToTime:kCMTimeZero];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top