首先iOS中对GIF图片的解析必须要用到的类库有:MobileCoreServices.framework 和ImageIO.framework ,在你的项目中添加这两个库,然后导入头文件。
文章主要讲到GIF图片的拆解,合成,包括在GIF图片上添加文字
主体思路:GIF图片拆解–>在每帧的图片上面添加文字–>然后合成GIF图片
下面为主要代码:
self.gifDicInfo = [self getGifInfoFromGifImageUrl:[NSURL URLWithString:kGIFImageUrl]];
//GIF 图片数组
NSArray *images = [self getImagesWithText:[self.gifDicInfo objectForKey:@"images"]];
//GIF 每帧延迟时间
NSArray *delays = [self.gifDicInfo objectForKey:@"delays"];
//是否循环
NSUInteger loopCount = [[self.gifDicInfo objectForKey:@"loopCount"] integerValue];
//创建图片路径
NSString *cashPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"animated.gif"];
CGImageDestinationRef destination =CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:cashPath], kUTTypeGIF, images.count, NULL);
NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:loopCount] forKey:(NSString *)kCGImagePropertyGIFLoopCount]forKey:(NSString *)kCGImagePropertyGIFDictionary]