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]
如何成为Android高级架构师!
架构师必须具备抽象思维和分析的能力,这是你进行系统分析和系统分解的基本素质。只有具备这样的能力,架构师才能看清系统的整体,掌控全局,这也是架构师大局观的形成基础。 你如何具备这种能力呢?一是来自于经验,二是来自于学习。
架构师不仅要具备在问题领域上的经验,也需要具备在软件工程领域内的经验。也就是说,架构师必须能够准确得理解需求,然后用软件工程的思想,把需求转化和分解成可用计算机语言实现的程度。经验的积累是需要一个时间过程的,这个过程谁也帮不了你,是需要你去经历的。
但是,如果你有意识地去培养,不断吸取前人的经验的话,还是可以缩短这个周期的。这也是我整理架构师进阶此系列的始动力之一。
成为Android架构师必备知识技能
对应导图的学习笔记(由阿里P8大牛手写,我负责整理成PDF笔记)
部分内容展示
《设计思想解读开源框架》