IOS:如何使用多张图片合成GIF

来源:互联网 发布:python的函数参数传递 编辑:程序博客网 时间:2024/04/27 17:07

使用多张PNG、JPG、JPEG等图片合成最终的动态GIF


 

UIImage *shacho = [UIImageimageNamed:@"2013_01_06_11_01_86.png"];

UIImage *bucho = [UIImageimageNamed:@"2013_01_06_11_01_93.png"];

    

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"animated4.gif"];

CGImageDestinationRef destination =CGImageDestinationCreateWithURL((CFURLRef)[NSURLfileURLWithPath:path], kUTTypeGIF, 2, NULL);

    

NSDictionary *frameProperties = [NSDictionarydictionaryWithObject:[NSDictionarydictionaryWithObject:[NSNumbernumberWithFloat:0.2f] forKey:(NSString *)kCGImagePropertyGIFDelayTime] forKey:(NSString *)kCGImagePropertyGIFDictionary];

NSDictionary *gifProperties = [NSDictionarydictionaryWithObject:[NSDictionarydictionaryWithObject:[NSNumbernumberWithInt:1] forKey:(NSString *)kCGImagePropertyGIFLoopCount]forKey:(NSString *)kCGImagePropertyGIFDictionary];

    

CGImageDestinationAddImage(destination, shacho.CGImage, (CFDictionaryRef)frameProperties);

    CGImageDestinationAddImage(destination, bucho.CGImage, (CFDictionaryRef)frameProperties);

    CGImageDestinationSetProperties(destination, (CFDictionaryRef)gifProperties);

    CGImageDestinationFinalize(destination);

    CFRelease(destination);

    NSLog(@"animated GIF file created at %@", path);

原创粉丝点击