版本记录
| 版本号 | 时间 | 
|---|---|
| V1.0 | 2017.07.18 | 
前言
在app中,有时候需要对gif图进行编辑,但是在进行帧编辑的前提是首先要获取到每一帧,这一篇就说一下方法。感兴趣的可以看看我写的其他小技巧。
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断
2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图
5.实用小技巧(五):通过相册或者相机更改图标
6.实用小技巧(六):打印ios里所有字体
7. 实用小技巧(七):UITableViewCell自适应行高的计算
8. 实用小技巧(八):数字余额显示的分隔
9.实用小技巧(九):类头条模糊背景的实现
10.实用小技巧(十):晃动手机换后台服务器网络
11.实用小技巧(十一):scrollView及其子类显示的一些异常处理
12.实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现
13.实用小技巧(十三):一种类酷我音乐盒动画实现
14.实用小技巧(十四):生成跳往applestore指定app的方法
15.实用小技巧(十五):左侧向右滑动返回上一级控制器
16.实用小技巧(十六):获取设备信息
17.实用小技巧(十七):清除缓存目录
功能需求
在app中,有时候需要对gif图进行编辑,但是在进行帧编辑的前提是首先要获取到每一帧,下面我们就说一种取出gif图中每一帧的方法,我们首先给出一个gif图。

gif图
功能实现
下面我们就直接看代码吧。
#import "JJGifVC.h"
#include <ImageIO/ImageIOBase.h>
#include <ImageIO/CGImageSource.h>
@interface JJGifVC ()
@property (nonatomic, strong) NSArray <UIImage *> *imageArr;
@end
@implementation JJGifVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.imageArr = [self loadGifImageArr];
    
    [self setupUI];
}
#pragma mark - Object Private Function
- (void)setupUI
{
    UIImageView *coverImageView = [[UIImageView alloc] init];
    coverImageView.layer.borderColor = [UIColor blueColor].CGColor;
    coverImageView.layer.borderWidth = 1.0;
    coverImageView.clipsToBounds = YES;
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    coverImageView.frame = CGRectMake(30.0, 100.0, 200, 200);
    [self.view addSubview:coverImageView];
    
    __weak typeof(self) weakSelf = self;
    NSInteger __block index = 0;
    NSTimer *timer = [NSTimer timerWithTimeInterval:0.8 repeats:YES block:^(NSTimer * _Nonnull timer) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        
        if (index < strongSelf.imageArr.count) {
            coverImageView.image = strongSelf.imageArr[index];
            index ++;
        }
        else {
            [timer invalidate];
            timer = nil;
        }
    }];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (NSArray *)loadGifImageArr
{
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"gif" withExtension:@"gif"];
    
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
    size_t gifCount = CGImageSourceGetCount(gifSource);
    NSMutableArray *frames = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        [frames addObject:image];
        CGImageRelease(imageRef);
    }
    return frames;
}
@end
这里我取出来gif的每一帧,然后给UIImageView赋上去,每隔0.8s更新一张,并做了防止数组越界和销毁定时器等功能。
效果实现
下面我们就看一下效果图。

静态效果图

gif动态效果图
可以看见,已经成功的取出来每一帧,并显示了出来。
后记
未完,待续~~~~~

花与冬