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);





你可能感兴趣的:(GIF图)