阿里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动画效果。
二、 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<imagesCount; i++) {
// CGImageSourceCreateImageAtIndex方法的作用是返回GIF中其中某一帧图像的CGImage类型数据。该方法有三个参数,参数1为GIF原始数据,参数2 为GIF子帧中的序号(该序号从0开始),参数3为GIF数据提取的一些选择参数,因为这里不是很常用,所以设置为nil。
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSourceRef, i, NULL);
// 以下为UIImage类的方法,这个方法用于实例化UIImage实例对象。该方法有三个参数,参数1为需要构建UIImage的内容,注意这里的内容是CGImage类型,参数2为手机物理像素与手机和手机显示分辨率的换算系数,参数3表明构建的UIImage的图像方向。通过这个方法就可以在某种手机分辨率下构建指定方向的图像,当然图像的类型是UIImage类型。
UIImage *image = [UIImage imageWithCGImage:imageRef scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
// 通过上述两步已经获取了UIImage,然而UIImage并不是通常我们看到的图像格式,此图像格式最大的特点是无法存储为本地可以查看的图片格式,因此如果需要将图像保存在本地,就需要在这之前将已经得到的UIImage数据类型转换为PNG或者JPG类型的图像数据,然后才能把图像存储到本地。
NSData *imageData = UIImagePNGRepresentation(image);
//生成保存路径
NSFileManager *fileManager = [NSFileManager defaultManager];//文件管理
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [path stringByAppendingPathComponent:@"yes"];
if (![fileManager fileExistsAtPath:filePath]) {//文件夹不存在就创建
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *gifPaths = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"gif%d.png",i]];
[imageData writeToFile:gifPaths atomically:YES];
}
}
三、 GIF图片的合成(序列图像合成GIF图像)
从功能上来说,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