iOS 播放Gif动画

//

//  loadGifView.h

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface loadGifView : UIView

/**

 *  @brief 初始化

 *

 *  @return

 */

- (instancetype)initWithFrame:(CGRect)frame localFileURL:(NSURL *)fileURL;

/**

 *  @brief 开始gif动画

 *

 *  @return

 */

- (void)startGifAnimation;

/**

 *  @brief 停止gif动画

 *

 *  @return

 */

- (void)stopGifAnimation;

/**

 *  @brief 得到gif中所有的图片的frame

 *

 *  @return

 */

+ (NSArray *)getFramesFromGif:(NSURL *)fileURL;

@end

//

//  loadGifView.m

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//  播放gif

#import "loadGifView.h"

#import <ImageIO/ImageIO.h>

#import <QuartzCore/CoreAnimation.h>

@interface loadGifView ()

@property (nonatomic, strong) NSMutableArray * frames;

@property (nonatomic, strong) NSMutableArray * frameDelayTimes;

@property (nonatomic, assign) CGFloat totalTime;

@property (nonatomic, assign) CGFloat width;

@property (nonatomic, assign) CGFloat height;

@end

@implementation loadGifView

/**

 * @brief  得到gif图片的信息

 *

 * @param

 * @return

 */

void getFrameInfo(CFURLRef url, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight)

{

    CGImageSourceRef gifSource = CGImageSourceCreateWithURL(url, NULL);

    

    // get frame count

    size_t frameCount = CGImageSourceGetCount(gifSource);

    for (size_t i = 0; i < frameCount; ++i) {

        // get each frame

        CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);

        [frames addObject:(__bridge id)frame];

        CGImageRelease(frame);

        

        // get gif info with each frame

        NSDictionary *dict = (NSDictionary*)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL));

        NSLog(@"kCGImagePropertyGIFDictionary %@", [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]);

        

        // get gif size

        if (gifWidth != NULL && gifHeight != NULL) {

            *gifWidth = [[dict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];

            *gifHeight = [[dict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];

        }

        

        // kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一样的

        NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];

        [delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];

        

        if (totalTime) {

            *totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];

        }

        

        CFRelease(CFBridgingRetain(dict));

    }

    

    if (gifSource) {

        CFRelease(gifSource);

    }

}

- (instancetype)initWithFrame:(CGRect)frame localFileURL:(NSURL *)fileURL

{

    self = [super initWithFrame:frame];

    if (self) {

        

        _frames = [[NSMutableArray alloc] init];

        _frameDelayTimes = [[NSMutableArray alloc] init];

        

        _width = 0;

        _height = 0;

        

        if (fileURL) {

            getFrameInfo((__bridge CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);

        }

    }

    

    return self;

}

+ (NSArray *)getFramesFromGif:(NSURL *)fileURL

{

    NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];

    NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];

    

    getFrameInfo((__bridge CFURLRef)fileURL, frames, delays, NULL, NULL, NULL);

    

    return frames;

}

- (void)startGifAnimation

{

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

    

    NSMutableArray *times = [NSMutableArray arrayWithCapacity:3];

    CGFloat currentTime = 0;

    int count = (int)_frameDelayTimes.count;

    for (int i = 0; i < count; ++i) {

        [times addObject:[NSNumber numberWithFloat:(currentTime / _totalTime)]];

        currentTime += [[_frameDelayTimes objectAtIndex:i] floatValue];

    }

    [animation setKeyTimes:times];

    

    NSMutableArray *images = [NSMutableArray arrayWithCapacity:3];

    for (int i = 0; i < count; ++i) {

        [images addObject:[_frames objectAtIndex:i]];

    }

    

    [animation setValues:images];

    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

    animation.duration = _totalTime;

    animation.delegate = self;

    animation.repeatCount = MAXFLOAT;

    

    [self.layer addAnimation:animation forKey:@"gifAnimation"];

}

- (void)stopGifAnimation

{

    [self.layer removeAllAnimations];

}

@end

//

//  PlayGifViewController.h

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface PlayGifViewController : UIViewController

@end

//

//  PlayGifViewController.m

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import "PlayGifViewController.h"

#import "loadGifView.h"

@interface PlayGifViewController ()

@property (nonatomic, strong) NSArray * imageArr;

@end

@implementation PlayGifViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor blackColor];

    

    // 播放gif动画

    NSString * path = [[NSBundle mainBundle] pathForResource:@"2.gif" ofType:nil];

    loadGifView * gifView = [[loadGifView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 200) / 2.0f, 100, 200, 300) localFileURL:[NSURL fileURLWithPath:path]];

    

    [gifView startGifAnimation];

    [self.view addSubview:gifView];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    

}

@end

 

原文地址:https://www.cnblogs.com/hanzhuzi/p/4461877.html