以下是 OC 代码 NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documenStr = [document objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *textDic= [documenStr stringByAppendingString:@"/gif"];
[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
NSString *path = [textDic stringByAppendingString:@"zomake.gif"];
NSLog(@"path = %@",path);
CGImageDestinationRef destion;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imagePathArray.count, NULL);
NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:(NSString *)kCGImagePropertyGIFDelayTime]
forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
[gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
[gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
[gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
[gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];
//单帧添加到 gif
for (UIImage *dimage in imagePathArray) {
CGImageDestinationAddImage(destion, dimage.CGImage,(__bridge CFDictionaryRef)frameDic);
}
CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(destion);
//此处解析 gif
NSData *data = [NSData dataWithContentsOfFile:path];
//此处将得到的 nsData 转为 gif 动画
UIImage *gifImage = [UIImage sd_tz_animatedGIFWithData:data];
望 V 友们给点思路。
1
andyzhshg 2017-04-19 12:39:50 +08:00
印象里 gif 是用的 256 色调色板来存储颜色的,所以每帧的画面至多有 256 种颜色,把真彩色的图片用 256 色表示,失真完全正常。
|
2
brance OP @andyzhshg 这个问题我也查阅了资料, 2 的八次方也就是 256 种颜色。超过了就出现了如述问题。
|
3
andyzhshg 2017-04-19 12:47:34 +08:00 1
@brance 好像是可以每帧用不同的调色板,这样的话针对每帧的颜色分布提取最佳的调色板,理论上是可以增强一些效果的,不过对于每帧色彩都比较丰富的图像,也是杯水车薪了。
|
4
deepjia 2017-04-19 12:49:13 +08:00 1
GIF 色彩少是常识
标题真是(不说脏话) |
5
robinray 2017-04-19 13:21:44 +08:00
楼主可以用 APNG 噢~~
|
7
bikkgry 2017-04-19 13:59:28 +08:00 via iPhone
....震惊部过来的吗
|
8
zhangsimon 2017-04-19 14:09:44 +08:00
如果只考虑应用内展示的话,强烈要求视频替换 gif
不仅色彩更丰富,同样的色彩采样和帧数,体积也更小 |
9
brance OP |
10
DT27 2017-04-20 08:35:44 +08:00
。。。。。。
|