在UIImageVIew上使用animationImages的有效方法

时间:2014-04-19 15:09:57

标签: ios objective-c animation uiimageview

我的应用程序中有一个15秒的动画,目前效率太低,无法使用,因为它使用1.5GB的RAM。

我是否错误地使用了UIImageView的动画属性?也许有一个不同的解决方案。我只想让它循环一段时间。

我可以对539张图片进行改进,使它们更有效,但它们仍然需要视网膜大小等。目前它是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];
}

1 个答案:

答案 0 :(得分:0)

感谢Mirko Brunner's条评论,我发现我可以使用嵌入式电影,而不是通过UIImageView播放动画,不过我最终使用AVPlayer而不是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];

}