GIF图的合成

1、创建图像目标

CGImageDestinationRef destination;

2、创建输出路径(保存的路径)

/*
     path
*/

3、创建CFURLRef对象

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);

4、通过一个url返回图像目标

destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, mArray.count, nil);

5、设置gif的信息,播放时隔事件,基本数据和delay事件

        NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.18],(NSString *)kCGImagePropertyGIFDelayTime, 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];
6、合成gif(把所有图片遍历添加到图像目标)

for (UIImage *dImg in mArray)
  {
         CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
  }

7、给gif添加信息

CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);

8、写入gif图

CGImageDestinationFinalize(destination);
9、释放目标图像

CFRelease(destination);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值