iOS gif制作
Posted Ven519
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS gif制作相关的知识,希望对你有一定的参考价值。
1、创建图像目标
[objc] view plain copy
- CGImageDestinationRef destination;
2、创建输出路径(保存的路径)
[objc] view plain copy
- /*
- path
- */
3、创建CFURLRef对象
[objc] view plain copy
- CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);
4、通过一个url返回图像目标
[objc] view plain copy
- destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, mArray.count, nil);
5、设置gif的信息,播放时隔事件,基本数据和delay事件
[objc] view plain copy
- NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.18],(NSString *)kCGImagePropertyGIFDelayTime, nil nil] forKey:(NSString *)kCGImagePropertyGIFDictionary];
- //设置gif信息
- NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
- [dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFImageColorMap];
- [dict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
- [dict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
- [dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
- NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyGIFDictionary];
[objc] view plain copy
- for (UIImage *dImg in mArray)
- CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
7、给gif添加信息
[objc] view plain copy
- CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
8、写入gif图
[objc] view plain copy
- CGImageDestinationFinalize(destination);
[objc] view plain copy
- CFRelease(destination);
以上是关于iOS gif制作的主要内容,如果未能解决你的问题,请参考以下文章