阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680
GIF的应用场景
GIF在iOS中的使用场景有以下三个方面。
(1)GIF图片分解为单帧图片。
(2)一系列单帧图片合成GIF图片。
(3)iOS系统上展示GIF动画效果。
在GIF的合成和分解方面将会接触到iOS图像处理核心框架ImageIO,作为iOS系统中图像处理的核心框架,它为我们提供了各种丰富的API,本文将要实现的GIF分解与合成功能,通过ImageIO就可以很方便地实现。GIF动画展示效果将结合UIImageView和定时器,利用逐帧展示的方式为大家呈现GIF动画效果。
整个过程划分为5个模块、4个过程,分别如下。
(1)本地读取GIF图片,将其转换为NSdata数据类型。
(2)将NSData作为ImageIO模块的输入。
(3)获取ImageIO的输出数据:UIImage。
(4)将获取到的UIImage数据存储为JPG或者PNG格式保存到本地。
- (void)viewDidLoad {
[super viewDidLoad];
//获取沙盒路径
NSLog(@"%@",NSHomeDirectory());
//gif的分解
NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"GIF1" ofType:@"gif"];
NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
CGImageSourceRef gifSourceRef = CGImageSourceCreateWithData((CFDataRef)gifData, nil);
size_t imagesCount = CGImageSourceGetCount(gifSourceRef);
for (int i = 0; i
从功能上来说,GIF图片的合成分为以下三个主要部分。
(1)加载待处理的67张原始数据源。
(2)在Document目录下构建GIF文件。
(3)设置GIF文件属性,利用ImageIO编码GIF文件。
/* 获取GIF图片信息 */
-(NSDictionary *)getGifInfoFromGifImagePath:(NSString *)filePath{
NSMutableArray *images = [NSMutableArray array]; // 图片数组
NSMutableArray *delays = [NSMutableArray array]; // 每帧对应的延迟时间
NSUInteger loopCount = 0; // 是否重复
CGFloat totalTime; // seconds
CGFloat width;
CGFloat height;
getFrameInfo((__bridge NSString *)((__bridge CFStringRef)(filePath)), images, delays, &totalTime, &width, &height, loopCount);
NSMutableDictionary *gifDic = [NSMutableDictionary dictionary];
gifDic[@"images"] = images;
gifDic[@"delays"] = delays;
gifDic[@"loopCount"] = @(loopCount);
gifDic[@"duration"] = @(totalTime);
gifDic[@"bounds"] = NSStringFromCGRect(CGRectMake(0, 0, width, height));
return gifDic;
}
/* GIF图片解析 */
void getFrameInfo(NSString * string, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight,NSUInteger loopCount)
{
NSData *data = [NSData dataWithContentsOfFile:string];
CGImageSourceRef gifSource = CGImageSourceCreateWithData((CFDataRef)data, nil);
//获取gif的帧数
size_t frameCount = CGImageSourceGetCount(gifSource);
//获取GfiImage的基本数据
NSDictionary *gifProperties = (__bridge NSDictionary *) CGImageSourceCopyProperties(gifSource, NULL);
//由GfiImage的基本数据获取gif数据
NSDictionary *gifDictionary =[gifProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary];
//获取gif的播放次数 0-无限播放
loopCount = [[gifDictionary objectForKey:(NSString*)kCGImagePropertyGIFLoopCount] integerValue];
CFRelease((__bridge CFTypeRef)(gifProperties));
for (size_t i = 0; i < frameCount; ++i) {
//得到每一帧的CGImage
CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
[frames addObject:[UIImage imageWithCGImage:frame]];
CGImageRelease(frame);
//获取每一帧的图片信息
NSDictionary *frameDict = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL);
//获取Gif图片尺寸
if (gifWidth != NULL && gifHeight != NULL) {
*gifWidth = [[frameDict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];
*gifHeight = [[frameDict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];
}
if (frameCount != 1) {
//由每一帧的图片信息获取gif信息
NSDictionary *gifDict = [frameDict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];
//取出每一帧的delaytime
[delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];
if (totalTime) {
*totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];
}
}
CFRelease((__bridge CFTypeRef)(frameDict));
}
CFRelease(gifSource);
}
根据相关参数设置合成GIF注意也可以直接生成data而不是保存到沙盒
NSArray *delays = [gifDicInfo objectForKey:@"delays"];
//是否循环
NSUInteger loopCount = [[gifDicInfo objectForKey:@"loopCount"] integerValue];
//创建图片路径
NSString *cashPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"animated11.gif"];
NSURL *url = [NSURL fileURLWithPath:cashPath];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)url, kUTTypeGIF, images.count, NULL);
// NSMutableData *data = [NSMutableData data];
// CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)data, kUTTypeGIF, images.count, NULL);
NSDictionary *gifDic = @{
(NSString *)kCGImagePropertyGIFLoopCount:[NSNumber numberWithInteger:loopCount]
};
NSDictionary *gifProperties = @{
(NSString *)kCGImagePropertyGIFDictionary:gifDic
};
for (int i = 0; i < images.count; i++) {
UIImage *image = [images objectAtIndex:i];
NSDictionary *gifDict = @{
(NSString *)kCGImagePropertyGIFDelayTime:[delays objectAtIndex:i]
};
NSDictionary *frameProperties = @{
(NSString *)kCGImagePropertyGIFDictionary:gifDict
};
CGImageDestinationAddImage(destination, image.CGImage, (CFDictionaryRef)frameProperties);
CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);
}
CGImageDestinationFinalize(destination);
CFRelease(destination);
NSLog(@"animated GIF file created at %@", cashPath);
NSMutableArray *array = [NSMutableArray array];
NSData *data = [NSData dataWithContentsOfFile:cashPath];
[array addObject:data];
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@"group.adfasf"];
[shared setObject:[array copy] forKey:@"data123"];
[shared synchronize];
原文链接:https://www.jianshu.com/p/3e3382d887fe
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680