/**
1. 数据获取
2. 创建Gif文件
3. 配置Gif属性
4. 单帧添加到gif
*/
github地址: https://github.com/mancongiOS/UIImage.git
导入:
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
实现:
// 1. 获取数据
NSMutableArray * imageArrayM = [NSMutableArray arrayWithCapacity:];
[imageArrayM addObject:[UIImage imageNamed:@"1.png"]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]];
[imageArrayM addObject:[UIImage imageNamed:@""]]; // 2. 创建Gif文件
NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentStr = [document objectAtIndex:];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * textDic = [documentStr stringByAppendingString:@"/gif"];
[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
NSString * path = [textDic stringByAppendingString:@"test1.gif"];
NSLog(@"path: %@",path); // 3. 配置gif属性
CGImageDestinationRef destion;
// 将path映射成url对象
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imageArrayM.count, NULL); NSMutableDictionary * dictM = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil];
NSDictionary * frameDic = [NSDictionary dictionaryWithObject:dictM forKey:(NSString *)kCGImagePropertyGIFDelayTime]; NSMutableDictionary * gifParaDict = [NSMutableDictionary dictionaryWithCapacity:]; // 设置颜色
[gifParaDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap]; // 设置模式
[gifParaDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel]; // 设置颜色深度
[gifParaDict setObject:[NSNumber numberWithInt:] forKey:(NSString *)kCGImagePropertyDepth]; // 是否可以重复播放
[gifParaDict setObject:[NSNumber numberWithInt:] forKey:(NSString *)kCGImagePropertyGIFLoopCount]; NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParaDict forKey:(NSString *)kCGImagePropertyGIFDictionary]; // 单帧添加到gif
for (UIImage * dImage in imageArrayM) {
CGImageDestinationAddImage(destion, dImage.CGImage, (__bridge CFDictionaryRef)frameDic);
} CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(url);
CFRelease(destion);