版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.07.18 |
前言
在app中,有時候需要對gif圖進行編輯,但是在進行幀編輯的前提是首先要獲取到每一幀,這一篇就說一下方法。感興趣的可以看看我寫的其他小技巧。
1. 實用小技巧(一):UIScrollView中上下左右滾動方向的判斷
2. 實用小技巧(二):屏幕橫豎屏的判斷和相關邏輯
3.實用小技巧(三):點擊手勢屏蔽子視圖的響應
4.實用小技巧(四):動態(tài)的增刪標簽視圖
5.實用小技巧(五):通過相冊或者相機更改圖標
6.實用小技巧(六):打印ios里所有字體
7. 實用小技巧(七):UITableViewCell自適應行高的計算
8. 實用小技巧(八):數(shù)字余額顯示的分隔
9.實用小技巧(九):類頭條模糊背景的實現(xiàn)
10.實用小技巧(十):晃動手機換后臺服務器網(wǎng)絡
11.實用小技巧(十一):scrollView及其子類顯示的一些異常處理
12.實用小技巧(十二):頭像圖片縮放以及保存到相冊簡單功能的實現(xiàn)
13.實用小技巧(十三):一種類酷我音樂盒動畫實現(xiàn)
14.實用小技巧(十四):生成跳往applestore指定app的方法
15.實用小技巧(十五):左側向右滑動返回上一級控制器
16.實用小技巧(十六):獲取設備信息
17.實用小技巧(十七):清除緩存目錄
功能需求
??在app中,有時候需要對gif圖進行編輯,但是在進行幀編輯的前提是首先要獲取到每一幀,下面我們就說一種取出gif圖中每一幀的方法,我們首先給出一個gif圖。
功能實現(xiàn)
下面我們就直接看代碼吧。
#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更新一張,并做了防止數(shù)組越界和銷毀定時器等功能。
效果實現(xiàn)
下面我們就看一下效果圖。
可以看見,已經(jīng)成功的取出來每一幀,并顯示了出來。
后記
未完,待續(xù)~~~~~